commit
76f2ec6c8a
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue