From 1b8e3834618f56b873dbb29c88addef064f4c1c6 Mon Sep 17 00:00:00 2001 From: jitendra Date: Tue, 12 Jul 2022 13:19:25 +0530 Subject: [PATCH] Added variable caching in product flat listener --- .../Product/src/Listeners/ProductFlat.php | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/Webkul/Product/src/Listeners/ProductFlat.php b/packages/Webkul/Product/src/Listeners/ProductFlat.php index 2528df611..b27bbba43 100644 --- a/packages/Webkul/Product/src/Listeners/ProductFlat.php +++ b/packages/Webkul/Product/src/Listeners/ProductFlat.php @@ -10,6 +10,7 @@ use Webkul\Product\Repositories\ProductFlatRepository; use Webkul\Product\Repositories\ProductAttributeValueRepository; use Webkul\Product\Helpers\ProductType; use Webkul\Product\Models\ProductAttributeValue; +use Webkul\Core\Repositories\ChannelRepository; class ProductFlat { @@ -160,15 +161,11 @@ class ProductFlat if (isset($product['channels'])) { foreach ($product['channels'] as $channel) { - $channel = app('Webkul\Core\Repositories\ChannelRepository')->findOrFail($channel); - - $channels[] = $channel['code']; + $channels[] = $this->getChannel($channel)->code; } } else if (isset($parentProduct['channels'])){ foreach ($parentProduct['channels'] as $channel) { - $channel = app('Webkul\Core\Repositories\ChannelRepository')->findOrFail($channel); - - $channels[] = $channel['code']; + $channels[] = $this->getChannel($channel)->code; } } else { $channels[] = core()->getDefaultChannelCode(); @@ -288,6 +285,21 @@ class ProductFlat } } + /** + * @param string $id + * @return mixed + */ + public function getChannel($id) + { + static $channels = []; + + if (isset($channels[$id])) { + return $channels[$id]; + } + + return $channels[$id] = app(ChannelRepository::class)->findOrFail($id); + } + /** * @param string $value * @return mixed