This commit is contained in:
jitendra 2022-09-14 16:45:16 +05:30
parent d6e8aa435a
commit 227ba07223
2 changed files with 2 additions and 142 deletions

View File

@ -33,21 +33,6 @@ class ProductFlatRepository extends Repository
return 'Webkul\Product\Contracts\ProductFlat';
}
/**
* Get category product model.
*
* @param int $categoryId
* @return \Illuminate\Support\Querybuilder
*/
public function categoryProductQueryBuilder($categoryId)
{
return $this->model
->leftJoin('product_categories', 'product_flat.product_id', 'product_categories.product_id')
->where('product_categories.category_id', $categoryId)
->where('product_flat.channel', core()->getCurrentChannelCode())
->where('product_flat.locale', app()->getLocale());
}
/**
* Update `product_flat` custom column.
*
@ -66,106 +51,6 @@ class ProductFlatRepository extends Repository
})->update(['product_flat.' . $attribute->code => \DB::raw($listener->attributeTypeFields[$attribute->type] . '_value')]);
}
/**
* Get category product attribute.
*
* @param int $categoryId
* @return array
*/
public function getCategoryProductAttribute($categoryId)
{
$qb = $this->categoryProductQueryBuilder($categoryId);
$childQuery = $this->model->distinct()->whereIn('parent_id', $qb->distinct()->select(['id']));
$attributeValues = $this->model
->distinct()
->leftJoin('product_attribute_values as pa', 'product_flat.product_id', 'pa.product_id')
->leftJoin('attributes as at', 'pa.attribute_id', 'at.id')
->leftJoin('product_super_attributes as ps', 'product_flat.product_id', 'ps.product_id')
->select('pa.integer_value', 'pa.text_value', 'pa.attribute_id', 'ps.attribute_id as attributeId')
->where('is_filterable', 1)
->where(function ($query) use ($qb, $childQuery) {
$query->whereIn('pa.product_id', $qb->distinct()->select(['product_flat.product_id']));
$query->orWhereIn('pa.product_id', $childQuery->select(['product_flat.product_id']));
})
->get();
$attributeInfo['attributeOptions'] = $attributeInfo['attributes'] = [];
foreach ($attributeValues as $attribute) {
$attributeKeys = array_keys($attribute->toArray());
foreach ($attributeKeys as $key) {
if (! is_null($attribute[$key])) {
if (
$key == 'integer_value'
&& ! in_array($attribute[$key], $attributeInfo['attributeOptions'])
) {
array_push($attributeInfo['attributeOptions'], $attribute[$key]);
} elseif (
$key == 'text_value'
&& ! in_array($attribute[$key], $attributeInfo['attributeOptions'])
) {
$multiSelectArrributes = explode(",", $attribute[$key]);
foreach ($multiSelectArrributes as $multi) {
if (! in_array($multi, $attributeInfo['attributeOptions'])) {
array_push($attributeInfo['attributeOptions'], $multi);
}
}
} elseif (
(
$key == 'attribute_id'
|| $key == 'attributeId'
)
&& ! in_array($attribute[$key], $attributeInfo['attributes'])
) {
array_push($attributeInfo['attributes'], $attribute[$key]);
}
}
}
}
return $attributeInfo;
}
/**
* Filter attributes according to products.
*
* @param \Webkul\Category\Contracts\Category $category
* @return \Illuminate\Support\Collection
*/
public function getProductsRelatedFilterableAttributes($category)
{
static $loadedCategoryAttributes = [];
if (array_key_exists($category->id, $loadedCategoryAttributes)) {
return $loadedCategoryAttributes[$category->id];
}
$productsCount = $this->categoryProductQueryBuilder($category->id)->count();
if ($productsCount > 0) {
$categoryFilterableAttributes = $category->filterableAttributes->pluck('id')->toArray();
$productCategoryAttributes = $this->getCategoryProductAttribute($category->id);
$allFilterableAttributes = array_filter(array_unique(array_intersect($categoryFilterableAttributes, $productCategoryAttributes['attributes'])));
$attributes = $this->attributeRepository->getModel()::with([
'options' => function ($query) use ($productCategoryAttributes) {
return $query->whereIn('id', $productCategoryAttributes['attributeOptions'])
->orderBy('sort_order');
}
])->whereIn('id', $allFilterableAttributes)->get();
return $loadedCategoryAttributes[$category->id] = $attributes;
} else {
return $loadedCategoryAttributes[$category->id] = $category->filterableAttributes;
}
}
/**
* Maximum price of category product.
*
@ -190,27 +75,6 @@ class ProductFlatRepository extends Repository
->max('max_price');
}
/**
* Get filter attributes.
*
* @param \Webkul\Category\Contracts\Category $category
* @return array
*/
public function getFilterAttributes($category)
{
$filterAttributes = [];
if (isset($category)) {
$filterAttributes = $this->getProductsRelatedFilterableAttributes($category);
}
if (empty($filterAttributes)) {
$filterAttributes = $this->attributeRepository->getFilterAttributes();
}
return $filterAttributes;
}
/**
* Handle category product max price.
*

View File

@ -108,13 +108,9 @@ class ProductController extends Controller
*/
public function getFilterAttributes($categoryId = null, AttributeRepository $attributeRepository)
{
$filterAttributes = [];
$category = $this->categoryRepository->findOrFail($categoryId);
if ($category = $this->categoryRepository->find($categoryId)) {
$filterAttributes = $this->productFlatRepository->getFilterAttributes($category);
}
if (empty($filterAttributes)) {
if (empty($filterAttributes = $category->filterableAttributes)) {
$filterAttributes = $attributeRepository->getFilterAttributes();
}