From 57e088b29ff929ed8df787acfa6022986ef23dbe Mon Sep 17 00:00:00 2001 From: "vishal.kushwaha650" Date: Tue, 6 Oct 2020 19:09:19 +0530 Subject: [PATCH 1/2] create function for attribute option related to product --- .../Repositories/ProductFlatRepository.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php index 2f33f4a82..72303594c 100644 --- a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php @@ -87,6 +87,39 @@ class ProductFlatRepository extends Repository return $filterAttributes; } + /** + * filter attribute option according to products + * + * @param array $filterAttributes + * @param array $products + * @return \Illuminate\Support\Collection + */ + public function productsRelatedAttributes($filterAttributes, $products) { + $allProductAttributeOptionsCode = []; + + foreach ($products as $key => $product) { + foreach ($filterAttributes as $attribute) { + if ($attribute->code <> 'price') { + if (isset($product[$attribute->code])) { + if (! in_array($product[$attribute->code], $allProductAttributeOptionsCode)) { + array_push($allProductAttributeOptionsCode, $product[$attribute->code]); + } + } + } + } + } + + foreach ($filterAttributes as $attribute) { + foreach ($attribute->options as $key => $option) { + if (! in_array($option->id, $allProductAttributeOptionsCode)) { + unset($attribute->options[$key]); + } + } + } + + return $filterAttributes; + + } /** * update product_flat custom column From 6ad00c46345b0f78b660d3952a713f4704b25fd1 Mon Sep 17 00:00:00 2001 From: "vishal.kushwaha650" Date: Wed, 7 Oct 2020 11:40:52 +0530 Subject: [PATCH 2/2] Issue #3950 fixed --- .../src/Repositories/ProductFlatRepository.php | 11 +++++++---- .../src/Repositories/ProductRepository.php | 17 +++++++++++++++++ .../products/list/layered-navigation.blade.php | 2 +- .../products/list/layered-navigation.blade.php | 2 +- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php index 72303594c..dc9027d3d 100644 --- a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php @@ -88,13 +88,16 @@ class ProductFlatRepository extends Repository } /** - * filter attribute option according to products + * filter attributes according to products * - * @param array $filterAttributes - * @param array $products + * @param array $category * @return \Illuminate\Support\Collection */ - public function productsRelatedAttributes($filterAttributes, $products) { + public function getProductsRelatedFilterableAttributes($category) { + $products = app('Webkul\Product\Repositories\ProductRepository')->getProductsRelatedToCategory($category->id); + + $filterAttributes = $this->getFilterableAttributes($category, $products); + $allProductAttributeOptionsCode = []; foreach ($products as $key => $product) { diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 8245ea1e3..53559b2a6 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -110,6 +110,23 @@ class ProductRepository extends Repository Event::dispatch('catalog.product.delete.after', $id); } + /** + * @param int $categoryId + * + * @return \Illuminate\Support\Collection + */ + public function getProductsRelatedToCategory($categoryId = null) + { + $qb = $this->model + ->leftJoin('product_categories', 'products.id', '=', 'product_categories.product_id'); + + if ($categoryId) { + $qb->where('product_categories.category_id', $categoryId); + } + + return $qb->get(); + } + /** * @param int $categoryId * diff --git a/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php b/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php index 3c8aadd3d..51030d51e 100755 --- a/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/products/list/layered-navigation.blade.php @@ -11,7 +11,7 @@ if (isset($category)) { $products = $productRepository->getAll($category->id); - $filterAttributes = $productFlatRepository->getFilterableAttributes($category, $products); + $filterAttributes = $productFlatRepository->getProductsRelatedFilterableAttributes($category); $maxPrice = core()->convertPrice($productFlatRepository->getCategoryProductMaximumPrice($category)); } diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php index 04251d924..a3fd0cb71 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/products/list/layered-navigation.blade.php @@ -9,7 +9,7 @@ if (isset($category)) { $products = $productRepository->getAll($category->id); - $filterAttributes = $productFlatRepository->getFilterableAttributes($category, $products); + $filterAttributes = $productFlatRepository->getProductsRelatedFilterableAttributes($category); $maxPrice = core()->convertPrice($productFlatRepository->getCategoryProductMaximumPrice($category)); }