This commit is contained in:
rahul shukla 2020-12-24 17:36:08 +05:30
parent 22ba6c9534
commit d09dcbf786
4 changed files with 27 additions and 95 deletions

View File

@ -43,9 +43,12 @@ class ProductFlatRepository extends Repository
->where('product_flat.channel', core()->getCurrentChannelCode())
->where('product_flat.locale', app()->getLocale());
$productArrributes = $qb->leftJoin('product_attribute_values as pa', 'product_flat.product_id', 'pa.product_id')
->pluck('pa.attribute_id')
->toArray();
$productArrributes = $qb
->leftJoin('product_attribute_values as pa', 'product_flat.product_id', 'pa.product_id')
->leftJoin('attributes as at', 'pa.attribute_id', 'at.id')
->where('is_filterable', 1)
->pluck('pa.attribute_id')
->toArray();
$productSuperArrributes = $qb->leftJoin('product_super_attributes as ps', 'product_flat.product_id', 'ps.product_id')
->pluck('ps.attribute_id')
@ -57,74 +60,30 @@ class ProductFlatRepository extends Repository
}
/**
* get Filterable Attributes.
* filter attributes according to products
*
* @param array $category
* @param array $products
* @return \Illuminate\Support\Collection
*/
public function getFilterableAttributes($category, $products) {
$filterAttributes = [];
public function getProductsRelatedFilterableAttributes($category)
{
$categoryFilterableAttributes = $category->filterableAttributes->pluck('id')->toArray();
if (count($category->filterableAttributes) > 0 ) {
$filterAttributes = $category->filterableAttributes;
} else {
$categoryProductAttributes = $this->getCategoryProductAttribute($category->id);
$productCategoryArrributes = $this->getCategoryProductAttribute($category->id);
if ($categoryProductAttributes) {
foreach (app('Webkul\Attribute\Repositories\AttributeRepository')->getFilterAttributes() as $filterAttribute) {
if (in_array($filterAttribute->id, $categoryProductAttributes)) {
$filterAttributes[] = $filterAttribute;
} else if ($filterAttribute ['code'] == 'price') {
$filterAttributes[] = $filterAttribute;
}
}
$allFilterableAttributes = array_filter(array_unique(array_merge($categoryFilterableAttributes, $productCategoryArrributes)));
$filterAttributes = collect($filterAttributes);
}
}
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' && 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;
$attributes = app('Webkul\Attribute\Repositories\AttributeRepository')
->whereIn('id', $allFilterableAttributes)
->with('options')
->get();
return $attributes;
}
/**
* update product_flat custom column
*
*
* @param \Webkul\Attribute\Models\Attribute $attribute
* @param \Webkul\Product\Listeners\ProductFlat $listener
*/
@ -137,5 +96,4 @@ class ProductFlatRepository extends Repository
->on('v.attribute_id', '=', \DB::raw($attribute->id));
})->update(['product_flat.'.$attribute->code => \DB::raw($listener->attributeTypeFields[$attribute->type] .'_value')]);
}
}

View File

@ -235,12 +235,14 @@ class ProductRepository extends Repository
if ($attribute->type != 'price') {
$attributeQuery->where(function ($attributeValueQuery) use ($column, $filterInputValues) {
foreach ($filterInputValues as $filterValue) {
if (! is_numeric($filterValue)) {
continue;
}
$attributeValueQuery->orWhereRaw("find_in_set(?, {$column})", [$filterValue]);
}
// foreach ($filterInputValues as $filterValue) {
// if (! is_numeric($filterValue)) {
// continue;
// }
// $attributeValueQuery->orWhereRaw("find_in_set(?, {$column})", [$filterValue]);
// }
$attributeValueQuery->whereIn($column, $filterInputValues);
});
} else {

View File

@ -9,8 +9,6 @@
$maxPrice = 0;
if (isset($category)) {
$products = $productRepository->getAll($category->id);
$filterAttributes = $productFlatRepository->getProductsRelatedFilterableAttributes($category);
$maxPrice = core()->convertPrice($productFlatRepository->getCategoryProductMaximumPrice($category));
@ -19,18 +17,6 @@
if (! count($filterAttributes) > 0) {
$filterAttributes = $attributeRepository->getFilterAttributes();
}
foreach ($filterAttributes as $attribute) {
if ($attribute->code <> 'price') {
if (! $attribute->options->isEmpty()) {
$attributes[] = $attribute;
}
} else {
$attributes[] = $attribute;
}
}
$filterAttributes = collect($attributes);
?>
<div class="layered-filter-wrapper">

View File

@ -7,28 +7,14 @@
$maxPrice = 0;
if (isset($category)) {
$products = $productRepository->getAll($category->id);
$filterAttributes = $productFlatRepository->getProductsRelatedFilterableAttributes($category);
$maxPrice = core()->convertPrice($productFlatRepository->getCategoryProductMaximumPrice($category));
}
}
if (! count($filterAttributes) > 0) {
$filterAttributes = $attributeRepository->getFilterAttributes();
}
foreach ($filterAttributes as $attribute) {
if ($attribute->code <> 'price') {
if (! $attribute->options->isEmpty()) {
$attributes[] = $attribute;
}
} else {
$attributes[] = $attribute;
}
}
$filterAttributes = collect($attributes);
?>
<div class="layered-filter-wrapper left">