I need to show Qty left message with qty of product in product detail page with below condition. If the available stock is less than 'Notify for Quantity Below' parameter of each product.

And can we show same message in product detail page More Information tab?

I have set Only X left Threshold as 15 in Store -> Configuration->Catalog->Inventory

I need to show That Qty left message message only if it is less than this value.

How this can be checked and show.

Please anyone suggest me in this?

  • How do you show configurable products qty? Commented Sep 11, 2018 at 11:23
  • Right now we are not using any configurable products Commented Sep 11, 2018 at 11:41

1 Answer 1


Create a block class to your custom module named Vendor_Module to below path:


and add the below content to it:

namespace Vendor\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
    protected $_scopeConfig;
    protected $_stockInterface;
    protected $_productRepository;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\CatalogInventory\Api\StockStateInterface $stockInterface,
        \Magento\Catalog\Model\ProductRepository $productRepository
        $this->_scopeConfig = $scopeConfig;
        $this->_stockInterface = $stockInterface;
        $this->_productRepository = $productRepository;

    public function getStockMessage($productId){
        $_product = $this->getProductById($productId);
        $_stock = $this->getStock($_product);
        if($_stock <= $this->getThresoldQty()){
            return __('Only %1 left', $_stock);
        return '';

    public function getProductById($id)
        return $this->_productRepository->getById($id);

    public function getStock($_product)
        return $this->_stockInterface->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());

    public function getThresoldQty(){
        return $this->_scopeConfig->getValue('cataloginventory/options/stock_threshold_qty', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

Now add the below code to top of your list.phtml

$blockObj = $block->getLayout()->createBlock('Vendor\Module\Block\Product');

Then call the below block inside your foreach loop:

<?php echo $blockObj->getStockMessage($_product->getId()); ?>

You can use the thing in your product details page.

  • this code will applicable to all products, but i need to check with condition and show, if stock is less than 'Notify for Quantity Below' parameter of the product and I need to show it More Information Tab of the product page. Commented Sep 11, 2018 at 11:46
  • This will give you the quantity now you can put your condition and render accordingly Commented Sep 11, 2018 at 11:54
  • Yes i got that code, but checking condition is only i am looking for, how can i check the condition for threshold qty? Commented Sep 11, 2018 at 11:57
  • ok wait I am updating my code Commented Sep 11, 2018 at 11:58
  • ok, please update me the code Commented Sep 11, 2018 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.