Back to Top

How to Display Left Quantity on Product Page in Magento 2

Updated 15 September 2025

“Hurry Up!! Only 5 Qty left in stock”

This Type of Marketing strategy is use for prompt potential customers to convert before their products run out of stock.

Why showing left quantity matters

Showing the stock left—“salable quantity”—creates urgency for customers.
Moreover, stores with multiple warehouses (using MSI) need correct stock display.

If product stock is hidden, it may mislead customers or cause lost sales.
Thus, it’s better to show real‐time remaining quantity under correct conditions.

Searching for an experienced
Magento 2 Company ?
Find out More

Prerequisites: Magento 2 MSI & Inventory Setup

Magento 2.3+ introduced Multi-Source Inventory (MSI).
For more details, check Adobe’s official guide on Inventory Management APIs.


This provides GetProductSalableQtyInterface and StockResolverInterface to compute correct stock.

Make sure MSI is enabled and sources are configured properly.
Otherwise, “salable quantity” may not reflect your actual warehouse inventory.

Left Qty Show

Salable quantity in Magento 2 represents the total stock available across all sources or warehouses.
It helps manage inventory centrally and prevents overselling or unexpected stockouts.

Get product salable quantity in Magento 2 and display left quantity on the product page!

Follow steps the below code to do so.

  1. Create registration.php at app/code/<vendor name>/<module name>/
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Webkul_LeftQty',
    __DIR__
);

2. Create module.xml file at app/code/<vendor name>/<module name>/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Webkul_LeftQty"/>
</config>

3. Create LeftQty.php file at app/code/<vendor name>/<module name>/Block/ 

<?php

namespace Webkul\LeftQty\Block;

use Magento\Catalog\Model\ProductFactory;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;

/**
 * Class LeftQty
 * @package Webkul\LeftQty\Block
 */
class LeftQty extends \Magento\Framework\View\Element\Template
{
    /**
     * @var GetProductSalableQtyInterface
     */
    protected $salebleqty;

    /**
     * @var StockResolverInterface
     */
    protected $stockresolver;

    /**
     * @var StoreManagerInterface
     */
    protected $storemanager;

    /**
     * @var Http
     */
    protected $request;

    /**
     * @var ProductFactory
     */
    protected $product;

    /**
     * LeftQty constructor.
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\App\Request\Http $request
     * @param ProductFactory $product
     * @param StoreManagerInterface $storemanager
     * @param GetProductSalableQtyInterface $salebleqty
     * @param StockResolverInterface $stockresolver
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\App\Request\Http $request,
        ProductFactory $product,
        StoreManagerInterface $storemanager,
        GetProductSalableQtyInterface $salebleqty,
        StockResolverInterface $stockresolver,
        array $data = [])
    {
        $this->request = $request;
        $this->product = $product;
        $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()
    {
        $productId = $this->request->getParam('id');
        $websiteCode = $this->storemanager->getWebsite()->getCode();
        $stockDetails = $this->stockresolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
        $stockId = $stockDetails->getStockId();
        $productDetails = $this->product->create()->load($productId);
        $sku = $productDetails->getSku();
        $proType = $productDetails->getTypeId();

        if ($proType != 'configurable' && $proType != 'bundle' && $proType != 'grouped') {
            $stockQty = $this->salebleqty->execute($sku, $stockId);
            return $stockQty;
        } else {
            return '';
        }

    }
}

4. Create catalog_product_view.xml file at app/code/<vendor name>/<module name>/view/frontend/layout/

<?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="Webkul\LeftQty\Block\LeftQty" name="catalog.product.view.leftqtyshow"
                   before="product.info.addtocart" template="Webkul_LeftQty::leftqty.phtml" cacheable="false"/>
        </referenceBlock>
    </body>
</page>

5. Create leftqty.phtml file at app/code/<vendor name>/<module name>/view/frontend/templates/

<?= "<span>".__("Hurry Up!! Only %1 Qty left in stock",$block->saleble()) . "</span>";?>

Conclusion

In conclusion, displaying left quantity on the product page under the conditions above improves transparency, urgency, and trust.

Moreover, by using Magento 2 MSI APIs and correct product-type checks, you ensure accurate stock information is shown.

That’s all

If any issue or doubt please feel free to mentioned in comment section.

I would be happy to help.

Happy Coding!!! 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • mong tiet
    • Kartik Upadhyay (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home