From ea3c0874c358a045cf60b8d309876f285ef86d7b Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Sat, 27 Feb 2021 18:42:43 +0530 Subject: [PATCH 1/3] Out Of Stock Query Updated For Configurable Product --- .../src/Repositories/ProductRepository.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 4da361477..6d666f79e 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -812,11 +812,22 @@ class ProductRepository extends Repository * @return Model */ public function checkOutOfStockItem($query) { - return $query->leftJoin('products as ps', 'product_flat.product_id', '=', 'ps.id') + return $query + ->leftJoin('products as ps', 'product_flat.product_id', '=', 'ps.id') ->leftJoin('product_inventories as pv', 'product_flat.product_id', '=', 'pv.product_id') ->where(function ($qb) { $qb - ->WhereIn('ps.type', ['configurable', 'grouped', 'downloadable', 'bundle', 'booking']) + ->where('ps.type', 'configurable') + ->whereRaw(' + (SELECT SUM(product_inventories.qty) + FROM product_flat + LEFT JOIN product_inventories ON product_inventories.product_id = product_flat.product_id + WHERE product_flat.parent_id = ps.id) > 0 + '); + }) + ->orWhere(function ($qb) { + $qb + ->WhereIn('ps.type', ['grouped', 'downloadable', 'bundle', 'booking']) ->orwhereIn('ps.type', ['simple', 'virtual'])->where('pv.qty' , '>' , 0); }); } From 13610a8dcf5ec74d948c89204003d362026db308 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Sat, 27 Feb 2021 19:04:31 +0530 Subject: [PATCH 2/3] DB Prefix Added --- .../Product/src/Repositories/ProductRepository.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 6d666f79e..0ace1e756 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -11,12 +11,12 @@ use Webkul\Core\Eloquent\Repository; use Illuminate\Support\Facades\Event; use Webkul\Attribute\Models\Attribute; use Webkul\Product\Models\ProductFlat; +use Illuminate\Support\Facades\Storage; use Illuminate\Container\Container as App; use Illuminate\Pagination\LengthAwarePaginator; use Webkul\Product\Models\ProductAttributeValueProxy; use Webkul\Attribute\Repositories\AttributeRepository; use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Facades\Storage; class ProductRepository extends Repository { @@ -819,10 +819,12 @@ class ProductRepository extends Repository $qb ->where('ps.type', 'configurable') ->whereRaw(' - (SELECT SUM(product_inventories.qty) - FROM product_flat - LEFT JOIN product_inventories ON product_inventories.product_id = product_flat.product_id - WHERE product_flat.parent_id = ps.id) > 0 + ( + SELECT SUM(' . DB::getTablePrefix() . 'product_inventories.qty) + FROM ' . DB::getTablePrefix() . 'product_flat + LEFT JOIN ' . DB::getTablePrefix() . 'product_inventories ON ' . DB::getTablePrefix() . 'product_inventories.product_id = ' . DB::getTablePrefix() . 'product_flat.product_id + WHERE ' . DB::getTablePrefix() . 'product_flat.parent_id = ps.id + ) > 0 '); }) ->orWhere(function ($qb) { From 65661e0f2533d3660821f590f1d993e1ec939b76 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Sun, 28 Feb 2021 23:46:19 +0530 Subject: [PATCH 3/3] Query Enhanced --- .../src/Repositories/ProductRepository.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 0ace1e756..3c0f0452e 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -818,14 +818,13 @@ class ProductRepository extends Repository ->where(function ($qb) { $qb ->where('ps.type', 'configurable') - ->whereRaw(' - ( - SELECT SUM(' . DB::getTablePrefix() . 'product_inventories.qty) - FROM ' . DB::getTablePrefix() . 'product_flat - LEFT JOIN ' . DB::getTablePrefix() . 'product_inventories ON ' . DB::getTablePrefix() . 'product_inventories.product_id = ' . DB::getTablePrefix() . 'product_flat.product_id - WHERE ' . DB::getTablePrefix() . 'product_flat.parent_id = ps.id - ) > 0 - '); + ->where(function ($qb) { + $qb + ->selectRaw('SUM(' . DB::getTablePrefix() . 'product_inventories.qty)') + ->from('product_flat') + ->leftJoin('product_inventories', 'product_inventories.product_id', '=', 'product_flat.product_id') + ->whereRaw(DB::getTablePrefix() . 'product_flat.parent_id = ps.id'); + }, '>', 0); }) ->orWhere(function ($qb) { $qb