From 18e4af0356bfc8e0aa5c3e98e4504c68901bc76c Mon Sep 17 00:00:00 2001 From: rahulshukla-home Date: Tue, 12 May 2020 14:23:41 +0530 Subject: [PATCH] Issue #3047 --- .gitignore | 2 +- .../Webkul/Product/src/Type/Configurable.php | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a19a6d127..6dc14fade 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,4 @@ yarn.lock storage/ storage/*.key /docker-compose-collection/ -/resources/themes/velocity/* +/resources/themes/velocity/* \ No newline at end of file diff --git a/packages/Webkul/Product/src/Type/Configurable.php b/packages/Webkul/Product/src/Type/Configurable.php index bd19a33f2..5301b0837 100644 --- a/packages/Webkul/Product/src/Type/Configurable.php +++ b/packages/Webkul/Product/src/Type/Configurable.php @@ -573,6 +573,8 @@ class Configurable extends AbstractType public function haveSufficientQuantity($qty) { $backorders = core()->getConfigData('catalog.inventory.stock_options.backorders'); + + $backorders = ! is_null ($backorders) ? $backorders : false; foreach ($this->product->variants as $variant) { if ($variant->haveSufficientQuantity($qty)) { @@ -598,4 +600,35 @@ class Configurable extends AbstractType return false; } + + /** + * @return int + */ + public function totalQuantity() + { + $total = 0; + + $channelInventorySourceIds = core()->getCurrentChannel() + ->inventory_sources() + ->where('status', 1) + ->pluck('id'); + + foreach ($this->product->variants as $variant) { + foreach ($variant->inventories as $inventory) { + if (is_numeric($index = $channelInventorySourceIds->search($inventory->inventory_source_id))) { + $total += $inventory->qty; + } + } + + $orderedInventory = $variant->ordered_inventories() + ->where('channel_id', core()->getCurrentChannel()->id) + ->first(); + + if ($orderedInventory) { + $total -= $orderedInventory->qty; + } + } + + return $total; + } } \ No newline at end of file