diff --git a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php index 2f33f4a82..dc9027d3d 100644 --- a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php @@ -87,6 +87,42 @@ class ProductFlatRepository extends Repository return $filterAttributes; } + /** + * filter attributes according to products + * + * @param array $category + * @return \Illuminate\Support\Collection + */ + public function getProductsRelatedFilterableAttributes($category) { + $products = app('Webkul\Product\Repositories\ProductRepository')->getProductsRelatedToCategory($category->id); + + $filterAttributes = $this->getFilterableAttributes($category, $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 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)); }