Merge pull request #4017 from vishal-webkul/issue3950

Issue 3950 Fixed
This commit is contained in:
Jitendra Singh 2020-10-08 15:39:20 +05:30 committed by GitHub
commit 76f2ec6c8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 2 deletions

View File

@ -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

View File

@ -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
*

View File

@ -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));
}

View File

@ -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));
}