Optimized product queries on category page

This commit is contained in:
jitendra 2022-08-08 20:05:38 +05:30
parent acf9626219
commit efded99220
2 changed files with 54 additions and 39 deletions

View File

@ -86,18 +86,20 @@ class ProductFlat
return false;
}
if (! in_array($attribute->code, $this->flatColumns)) {
Schema::table('product_flat', function (Blueprint $table) use($attribute) {
$table->{$this->attributeTypeFields[$attribute->type]}($attribute->code)->nullable();
if (
$attribute->type == 'select'
|| $attribute->type == 'multiselect'
) {
$table->string($attribute->code . '_label')->nullable();
}
});
if (in_array($attribute->code, $this->flatColumns)) {
return;
}
Schema::table('product_flat', function (Blueprint $table) use($attribute) {
$table->{$this->attributeTypeFields[$attribute->type]}($attribute->code)->nullable();
if (
$attribute->type == 'select'
|| $attribute->type == 'multiselect'
) {
$table->string($attribute->code . '_label')->nullable();
}
});
}
/**
@ -110,21 +112,22 @@ class ProductFlat
{
$attribute = $this->attributeRepository->find($attributeId);
if (in_array(strtolower($attribute->code), $this->flatColumns)) {
Schema::table('product_flat', function (Blueprint $table) use($attribute) {
$table->dropColumn($attribute->code);
if (
$attribute->type == 'select'
|| $attribute->type == 'multiselect'
) {
$table->dropColumn($attribute->code . '_label');
}
});
$this->productFlatRepository->updateAttributeColumn( $attribute , $this);
if (! in_array(strtolower($attribute->code), $this->flatColumns)) {
return;
}
Schema::table('product_flat', function (Blueprint $table) use($attribute) {
$table->dropColumn($attribute->code);
if (
$attribute->type == 'select'
|| $attribute->type == 'multiselect'
) {
$table->dropColumn($attribute->code . '_label');
}
});
$this->productFlatRepository->updateAttributeColumn( $attribute , $this);
}
/**
@ -137,10 +140,12 @@ class ProductFlat
{
$this->createFlat($product);
if (ProductType::hasVariants($product->type)) {
foreach ($product->variants()->get() as $variant) {
$this->createFlat($variant, $product);
}
if (! ProductType::hasVariants($product->type)) {
return;
}
foreach ($product->variants()->get() as $variant) {
$this->createFlat($variant, $product);
}
}
@ -192,8 +197,11 @@ class ProductFlat
]);
foreach ($familyAttributes[$product->attribute_family_id] as $attribute) {
if (($parentProduct
&& ! in_array($attribute->code, array_merge($superAttributes[$parentProduct->id], $this->fillableAttributeCodes)))
if (
(
$parentProduct
&& ! in_array($attribute->code, array_merge($superAttributes[$parentProduct->id], $this->fillableAttributeCodes))
)
|| in_array($attribute->code, ['tax_category_id'])
|| ! in_array($attribute->code, $this->flatColumns)
) {
@ -265,10 +273,10 @@ class ProductFlat
if ($parentProduct) {
$parentProductFlat = $this->productFlatRepository->findOneWhere([
'product_id' => $parentProduct->id,
'channel' => $channel->code,
'locale' => $locale->code,
]);
'product_id' => $parentProduct->id,
'channel' => $channel->code,
'locale' => $locale->code,
]);
if ($parentProductFlat) {
$productFlat->parent_id = $parentProductFlat->id;

View File

@ -24,7 +24,6 @@ class ProductRepository extends Repository
*
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
* @param \Illuminate\Container\Container $container
*
* @return void
*/
public function __construct(
@ -157,7 +156,15 @@ class ProductRepository extends Repository
$page = Paginator::resolveCurrentPage('page');
$repository = app(ProductFlatRepository::class)->scopeQuery(function ($query) use ($params, $categoryId) {
$repository = app(ProductFlatRepository::class)->with([
'images',
'product.videos',
'product.attribute_values',
'product.customer_group_prices',
'product.inventory_sources',
'product.inventories',
'product.ordered_inventories',
])->scopeQuery(function ($query) use ($params, $categoryId) {
$channel = core()->getRequestedChannelCode();
$locale = core()->getRequestedLocaleCode();
@ -273,7 +280,6 @@ class ProductRepository extends Repository
$qb->where(function ($filterQuery) use ($attributeFilters) {
foreach ($attributeFilters as $attribute) {
$filterQuery->orWhere(function ($attributeQuery) use ($attribute) {
$column = DB::getTablePrefix() . 'product_attribute_values.' . ProductAttributeValueProxy::modelClass()::$attributeTypeFields[$attribute->type];
$filterInputValues = explode(',', request()->get($attribute->code));
@ -289,6 +295,7 @@ class ProductRepository extends Repository
if (! is_numeric($filterValue)) {
continue;
}
$attributeValueQuery->orWhereRaw("find_in_set(?, {$column})", [$filterValue]);
}
});
@ -526,7 +533,7 @@ class ProductRepository extends Repository
->leftJoin('product_inventories as pv', 'product_flat.product_id', '=', 'pv.product_id')
->where(function ($qb) {
return $qb
/* for grouped, downloadable, bundle and booking product */
/* for grouped, downloadable, bundle and booking product */
->orWhereIn('ps.type', ['grouped', 'downloadable', 'bundle', 'booking'])
/* for simple and virtual product */
->orWhere(function ($qb) {