diff --git a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php index 4f1bd776d..db994b0a8 100644 --- a/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductFlatRepository.php @@ -43,88 +43,64 @@ 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->distinct() + ->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); - $productSuperArrributes = $qb->leftJoin('product_super_attributes as ps', 'product_flat.product_id', 'ps.product_id') + $productArrributesIds = $productArrributes->pluck('pa.attribute_id')->toArray(); + + $productSelectArrributes = $productArrributes + ->pluck('integer_value') + ->toArray(); + + $productmultiSelectArrributes = $productArrributes + ->pluck('text_value') + ->toArray(); + + $multiSelectArrributes = []; + foreach ($productmultiSelectArrributes as $multi) { + if ($multi) { + $multiSelectArrributes = explode(",", $multi); + } + } + + $productSuperArrributesIds = $qb->leftJoin('product_super_attributes as ps', 'product_flat.product_id', 'ps.product_id') ->pluck('ps.attribute_id') ->toArray(); - $productCategoryArrributes = array_unique(array_merge($productArrributes, $productSuperArrributes)); + $productCategoryArrributes['attributeOptions'] = array_filter(array_unique(array_merge($productSelectArrributes, $multiSelectArrributes))); + + $productCategoryArrributes['attributes'] = array_filter(array_unique(array_merge($productArrributesIds, $productSuperArrributesIds))); return $productCategoryArrributes; } /** - * 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['attributes']))); - $filterAttributes = collect($filterAttributes); + $attributes = app('Webkul\Attribute\Repositories\AttributeRepository')->getModel()::with(['options' => function($query) use ($productCategoryArrributes) { + return $query->whereIn('id', $productCategoryArrributes['attributeOptions']); } - } - - 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; + ])->whereIn('id', $allFilterableAttributes)->get(); + return $attributes; } /** * update product_flat custom column - * + * * @param \Webkul\Attribute\Models\Attribute $attribute * @param \Webkul\Product\Listeners\ProductFlat $listener */ @@ -137,5 +113,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')]); } - } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index e6776d091..7a7f09921 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -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 { 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 51030d51e..96ebd303c 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 @@ -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); ?>
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 fa5d580ad..137ad209d 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 @@ -7,8 +7,6 @@ $maxPrice = 0; if (isset($category)) { - $products = $productRepository->getAll($category->id); - $filterAttributes = $productFlatRepository->getProductsRelatedFilterableAttributes($category); $maxPrice = core()->convertPrice($productFlatRepository->getCategoryProductMaximumPrice($category)); @@ -17,18 +15,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); ?>