How to Show Salable Quantity in Magento 2 Product Page
“Only 2 left in stock!”
Ever read it on any online store? If yes, ever felt a sense of urgency and FOMO?
Marketers use this strategy to prompt potential customers to convert before their favorite products run out of stock.
However, have you ever thought about how to implement such a technique in Magento 2 store?
The answer is to show salable quantity in Magento 2 product page.
As the release of Magento 2.3.3 introduced the concept of salable quantity with the Magento 2 Multi-Source Inventory system, now Magento 2 store owners can make use of it and add such messages on the frontend as shown here:
Salable quantity is the sum of all available products that allow you to manage multiple warehouses from one place and saves you from dead inventory or out of stock!
Get product salable quantity in Magento 2 and show salable quantity on the product page!
Check out the below code to do so.
Method to Show Salable Quantity in Magento 2 Product Page
- Create registration.php at app/code/Vendor/Module/
123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'Meetanshi_ShowQty',__DIR__); - Create module.xml file at app/code/Vendor/Module/etc
12345<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"><module name="Meetanshi_ShowQty" setup_version="1.0.0" schema_version="1.0.0"/></config> - Create routes.xml file at app/code/Vendor/Module/etc/frontend
123456789<?xml version="1.0" ?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"><router id="standard"><route frontName="showqty" id="showqty"><module name="Meetanshi_ShowQty"/></route></router></config> - Create catalog_product_view.xml file at app/code/Vendor/Module/view/frontend/layout
12345678910<?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="Magento\Framework\View\Element\Template" name="catalog.product.view.extrablock"before="product.info.addtocart" template="Meetanshi_ShowQty::showqty.phtml" cacheable="false"/></referenceBlock></body></page> - Create showqty.phtml file at app/code/Vendor/Module/view/frontend/templates
1234<?php$blockObj = $block->getLayout()->createBlock('Meetanshi\ShowQty\Block\ShowQty');echo "<h1>Salable Qty:- " . $blockObj->saleble() . "</h1>";?> - Create showqty.php file at app/code/Vendor/Module/Block and use the below code:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495<?phpnamespace Meetanshi\ShowQty\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 ShowQty* @package Meetanshi\ShowQty\Block*/class ShowQty extends Template{/*** @var GetProductSalableQtyInterface*/protected $salebleqty;/*** @var StockResolverInterface*/protected $stockresolver;/*** @var StoreManagerInterface*/protected $storemanager;/*** @var Http*/protected $request;/*** @var ProductFactory*/protected $product;/*** ShowQty constructor.* @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);}/*** @throws \Magento\Framework\Exception\LocalizedException* @throws \Magento\Framework\Exception\NoSuchEntityException*/public function saleble(){$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 '';}}}
That’s it. Get Salable Quantity information after product is saved in Magento 2 to maintain the well structured inventory grid.
If you have questions regarding this post, feel free to ask in the Comments section below.
I would be happy to answer.
Do consider sharing this post with Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
10 Best Payment Gateways in UK [2024]
Best Social Media Management Tools A Marketer Must Know
Next