From 2552900d62ffadd2febc4bb444ff9ed0581bd692 Mon Sep 17 00:00:00 2001 From: rahul shukla Date: Fri, 26 Feb 2021 12:26:12 +0530 Subject: [PATCH 1/4] Issue #4682 fixed --- .../Product/src/Repositories/ProductRepository.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index dbbc10a50..44e421311 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -157,6 +157,7 @@ class ProductRepository extends Repository ->join('product_flat as variants', 'product_flat.id', '=', DB::raw('COALESCE(' . DB::getTablePrefix() . 'variants.parent_id, ' . DB::getTablePrefix() . 'variants.id)')) ->leftJoin('product_categories', 'product_categories.product_id', '=', 'product_flat.product_id') ->leftJoin('product_attribute_values', 'product_attribute_values.product_id', '=', 'variants.product_id') + ->leftJoin('catalog_rule_product_prices', 'catalog_rule_product_prices.product_id', '=', 'variants.product_id') ->where('product_flat.channel', $channel) ->where('product_flat.locale', $locale) ->whereNotNull('product_flat.url_key'); @@ -212,8 +213,17 @@ class ProductRepository extends Repository if ($priceFilter = request('price')) { $priceRange = explode(',', $priceFilter); if (count($priceRange) > 0) { - $qb->where('variants.min_price', '>=', core()->convertToBasePrice($priceRange[0])); - $qb->where('variants.min_price', '<=', core()->convertToBasePrice(end($priceRange))); + + $qb->where(function ($query) use($priceRange) { + $query->where(function ($query) use($priceRange) { + $query->where('variants.min_price', '>=', core()->convertToBasePrice($priceRange[0])); + $query->where('variants.min_price', '<=', core()->convertToBasePrice(end($priceRange))); + }) + ->orWhere(function($query) use($priceRange) { + $query->where('catalog_rule_product_prices.price', '>=', core()->convertToBasePrice($priceRange[0])); + $query->where('catalog_rule_product_prices.price', '<=', core()->convertToBasePrice(end($priceRange))); + }); + }); } } From 20d3601ff12bb1e44670eaf090a19f04e4138234 Mon Sep 17 00:00:00 2001 From: rahul shukla Date: Fri, 26 Feb 2021 12:29:06 +0530 Subject: [PATCH 2/4] merge with master --- .../Webkul/Product/src/Repositories/ProductRepository.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 665547e49..6a3d838e9 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -215,11 +215,11 @@ class ProductRepository extends Repository if (count($priceRange) > 0) { $qb->where(function ($query) use($priceRange) { - $query->where(function ($query) use($priceRange) { + $query->where(function ($query) use ($priceRange) { $query->where('variants.min_price', '>=', core()->convertToBasePrice($priceRange[0])); $query->where('variants.min_price', '<=', core()->convertToBasePrice(end($priceRange))); }) - ->orWhere(function($query) use($priceRange) { + ->orWhere(function($query) use ($priceRange) { $query->where('catalog_rule_product_prices.price', '>=', core()->convertToBasePrice($priceRange[0])); $query->where('catalog_rule_product_prices.price', '<=', core()->convertToBasePrice(end($priceRange))); }); From 872e936daa96ebcf725c791e7da9ea3077b6e295 Mon Sep 17 00:00:00 2001 From: rahul shukla Date: Fri, 26 Feb 2021 14:02:36 +0530 Subject: [PATCH 3/4] Query optimized --- .../Product/src/Repositories/ProductRepository.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 6a3d838e9..2635c37c2 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -157,7 +157,6 @@ class ProductRepository extends Repository ->join('product_flat as variants', 'product_flat.id', '=', DB::raw('COALESCE(' . DB::getTablePrefix() . 'variants.parent_id, ' . DB::getTablePrefix() . 'variants.id)')) ->leftJoin('product_categories', 'product_categories.product_id', '=', 'product_flat.product_id') ->leftJoin('product_attribute_values', 'product_attribute_values.product_id', '=', 'variants.product_id') - ->leftJoin('catalog_rule_product_prices', 'catalog_rule_product_prices.product_id', '=', 'variants.product_id') ->where('product_flat.channel', $channel) ->where('product_flat.locale', $locale) ->whereNotNull('product_flat.url_key'); @@ -214,16 +213,9 @@ class ProductRepository extends Repository $priceRange = explode(',', $priceFilter); if (count($priceRange) > 0) { - $qb->where(function ($query) use($priceRange) { - $query->where(function ($query) use ($priceRange) { - $query->where('variants.min_price', '>=', core()->convertToBasePrice($priceRange[0])); - $query->where('variants.min_price', '<=', core()->convertToBasePrice(end($priceRange))); - }) - ->orWhere(function($query) use ($priceRange) { - $query->where('catalog_rule_product_prices.price', '>=', core()->convertToBasePrice($priceRange[0])); - $query->where('catalog_rule_product_prices.price', '<=', core()->convertToBasePrice(end($priceRange))); - }); - }); + $qb->leftJoin('catalog_rule_product_prices', 'catalog_rule_product_prices.product_id', '=', 'variants.product_id') + ->where(DB::raw('(CASE WHEN catalog_rule_product_prices.price > 0 THEN catalog_rule_product_prices.price ELSE variants.min_price END)'), '>=', core()->convertToBasePrice($priceRange[0])) + ->where(DB::raw('(CASE WHEN catalog_rule_product_prices.price > 0 THEN catalog_rule_product_prices.price ELSE variants.min_price END)'), '<=', core()->convertToBasePrice(end($priceRange))); } } From 53b1447220cc2c3d3fc32aad0a987ad80966cbae Mon Sep 17 00:00:00 2001 From: rahul shukla Date: Fri, 26 Feb 2021 14:31:51 +0530 Subject: [PATCH 4/4] database prefix added --- .../Webkul/Product/src/Repositories/ProductRepository.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 2635c37c2..3cb27e78a 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -213,9 +213,11 @@ class ProductRepository extends Repository $priceRange = explode(',', $priceFilter); if (count($priceRange) > 0) { + $priceQuery = DB::raw('(CASE WHEN ' . DB::getTablePrefix() . 'catalog_rule_product_prices.price > 0 THEN ' . DB::getTablePrefix() . 'catalog_rule_product_prices.price ELSE ' . DB::getTablePrefix() . 'variants.min_price END)'); + $qb->leftJoin('catalog_rule_product_prices', 'catalog_rule_product_prices.product_id', '=', 'variants.product_id') - ->where(DB::raw('(CASE WHEN catalog_rule_product_prices.price > 0 THEN catalog_rule_product_prices.price ELSE variants.min_price END)'), '>=', core()->convertToBasePrice($priceRange[0])) - ->where(DB::raw('(CASE WHEN catalog_rule_product_prices.price > 0 THEN catalog_rule_product_prices.price ELSE variants.min_price END)'), '<=', core()->convertToBasePrice(end($priceRange))); + ->where($priceQuery, '>=', core()->convertToBasePrice($priceRange[0])) + ->where($priceQuery, '<=', core()->convertToBasePrice(end($priceRange))); } }