From ecfe1f635417e05ed362f072da3d765094790255 Mon Sep 17 00:00:00 2001 From: Bruno Dias Date: Tue, 18 Jun 2019 16:22:08 +0100 Subject: [PATCH] Added functionality to work with Salable Qty on mage 2.3 --- Block/Product/View/Type/Configurable.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Block/Product/View/Type/Configurable.php b/Block/Product/View/Type/Configurable.php index 073079a..cd37ed9 100644 --- a/Block/Product/View/Type/Configurable.php +++ b/Block/Product/View/Type/Configurable.php @@ -18,6 +18,7 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\Locale\Format; use Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory; +use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface; /** * Class Configurable @@ -52,6 +53,10 @@ class Configurable extends SwatchConfigurable * @var StockItemInterfaceFactory */ private $stockItemInterfaceFactory; + /** + * @var GetProductSalableQtyInterface + */ + private $getProductSalableQtyInterface; /** * @param Context $context @@ -69,6 +74,7 @@ class Configurable extends SwatchConfigurable * @param SwatchAttributesProvider $swatchAttributesProvider * @param Format|null $localeFormat * @param StockItemInterfaceFactory $stockItemInterfaceFactory + * @param GetProductSalableQtyInterface $getProductSalableQtyInterface * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( @@ -86,7 +92,8 @@ public function __construct( array $data = [], SwatchAttributesProvider $swatchAttributesProvider = null, Format $localeFormat = null, - StockItemInterfaceFactory $stockItemInterfaceFactory + StockItemInterfaceFactory $stockItemInterfaceFactory, + GetProductSalableQtyInterface $getProductSalableQtyInterface ){ $this->swatchHelper = $swatchHelper; $this->swatchMediaHelper = $swatchMediaHelper; @@ -111,6 +118,7 @@ public function __construct( ); $this->context = $context; $this->stockItemInterfaceFactory = $stockItemInterfaceFactory; + $this->getProductSalableQtyInterface = $getProductSalableQtyInterface; } /** @@ -146,9 +154,17 @@ public function getProductStock() foreach ($allProducts as $product) { if ($product->isSaleable() || $skipSaleableCheck) { - $stock[$product->getId()] = $this->getStockItem($product->getId()); + + //Check salable quantity + $salableQty = $this->getProductSalableQtyInterface->execute($product->getSku(), 1); + if ($salableQty >= 1) { + $stock[$product->getId()] = $this->getStockItem($product->getId()); + } else { + $stock[$product->getId()]['out_stock'] = 1; + } } } + return $stock; } @@ -191,4 +207,4 @@ public function getAllowProducts() return $this->getData('allow_products'); } -} \ No newline at end of file +}