Magento 2 Show Salable Quantity In Product Page in Magento 2.3.x version. After Magento 2.3.x version, you might notice that Magento display 2 different qty salable quantity.
The quantity of product will not decrease when you just place an order. Salable Quantity is the sum of available resources, grouped in stocks. you can get value by calling getSalabelQty() function in your module file. You need to pass the parameter of product SKU in the function.
Read This: How To Get Product Salable Quantity In Magento 2
We are already learned how to create a basic module in Magento 2. We need to create module.xml and registration.php files. You can create module.xml and registration.php from this tutorial.
Here we are using Dolphin as Vendor name and MyModule as the name of a module. You can change this according to your Vendor and Module name.
Create layout file catalog_product_view.xml in app/code/Dolphin/MyModule/view/frontend/layout folder with the following code.
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="alert.urls"> <block class="Dolphin\MyModule\Block\LeftQty" name="catalog.product.view.stock" before="product.info.addtocart" template="Dolphin_MyModule::leftqty.phtml" cacheable="false"/> </referenceBlock> </body> </page>
Create layout file leftqty.phtml in app/code/Dolphin/MyModule/view/frontend/templates folder with the following code.
<?php /** @var $block Dolphin\MyModule\Block\LeftQty */ ?> <div class="left-stock"> <strong> <?= ($block->salebleqty() != '') ? $block->escapeHtml(__('Only %1 left in stock!', $block->salebleqty())) : '' ?> </strong> </div>
Create Block file LeftQty.php in app/code/Dolphin/MyModule/Block folder with the following code.
<?php namespace Dolphin\MyModule\Block; use Magento\Catalog\Model\ProductFactory; use Magento\Framework\App\Request\Http; use Magento\Framework\View\Element\Template; use Magento\Backend\Block\Template\Context; use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface; use Magento\InventorySalesApi\Api\StockResolverInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; class LeftQty extends Template { /** * @var GetProductSalableQtyInterface */ protected $salebleqty; /** * @var StockResolverInterface */ protected $stockresolver; /** * @var StoreManagerInterface */ protected $storemanager; /** * @var Http */ protected $request; /** * @var ProductFactory */ protected $product; /** * @param ProductFactory $product * @param StoreManagerInterface $storemanager * @param GetProductSalableQtyInterface $salebleqty * @param Http $request * @param StockResolverInterface $stockresolver * @param Context $context * @param array $data */ public function __construct( ProductFactory $product, StoreManagerInterface $storemanager, GetProductSalableQtyInterface $salebleqty, Http $request, StockResolverInterface $stockresolver, Context $context, array $data = []) { $this->product = $product; $this->request = $request; $this->storemanager = $storemanager; $this->salebleqty = $salebleqty; $this->stockresolver = $stockresolver; parent::__construct($context, $data); } public function salebleqty() { $websiteCode = $this->storemanager->getWebsite()->getCode(); $stock = $this->stockresolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode); $stockId = $stock->getStockId(); $productid = $this->request->getParam('id'); $loadProduct = $this->product->create()->load($productid); $sku = $loadProduct->getSku(); $type = $loadProduct->getTypeId(); if ($type != 'configurable' && $type != 'bundle' && $type != 'grouped') { $stockQty = $this->salebleqty->execute($sku, $stockId); return $stockQty; } else { return ''; } } }
Above Code see $this->salebleqty->execute($sku, $stockId) First parameter is a Product SKU and second one is stock id.you can get $stockId using $stock->getStockId();
Now execute below command and referesh the page:
[dt_highlight color="" text_color="" bg_color=""]php bin/magento s:up[/dt_highlight] [dt_highlight color="" text_color="" bg_color=""]php bin/magento s:s:d -f[/dt_highlight] [dt_highlight color="" text_color="" bg_color=""]php bin/magento c:c[/dt_highlight]
Result :
I Hope, This instruction will be helpful for you.
If you have any difficulties regarding this blog, do consider them posting in the Comments section below!
I’m here to help.
Thank you!