I need on configurable product page to show available inventory of simple products,while selecting options, like price will change on option change in same way.


Finally I found a way to show awailable QTY:

Override configurable.js:


Add :


In this function :


and paste this code in your PHTML

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    $productTypeInstance = $product->getTypeInstance();
   if ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
    $usedProducts = $productTypeInstance->getUsedProducts($product);

        foreach ($usedProducts  as $child) {?>
            <li class="myli" id="div<?php echo $child->getId()?>" style="display:none;list-style:none">
            $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($child->getId());
            echo "We have ".round($productStockObj->getData('qty'))." items in stock!";

 <?php    }   ?>

 </ul> <?php }else{
     $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($product->getId());
     echo "We have ".round($productStockObj->getData('qty'))." items in stock!";
| improve this answer | |
  • Hi, can you please share the layout XML. I am implementing the same for one of my projects. – Kalyan Chakravarthi V Jul 2 '18 at 14:06
  • No need to edit in layout.xml, Use above code in your phtml directly where you need to show – Ram Pratap Singh Gour Jul 3 '18 at 5:24
  • Ok thanks, in our project swatches are there so doing changes in configurable.js is not working. Do you know where to change if swatches are there? – Kalyan Chakravarthi V Jul 3 '18 at 5:27
  • Swatches should have their JS, Which will work on click on swatches, You have to check that JS and put above JS code there. – Ram Pratap Singh Gour Jul 3 '18 at 5:30
  • Use this answer for configurable swatches - magento.stackexchange.com/questions/291072/… – Ranganathan Sep 27 '19 at 9:39

I'm new to magento 2 .3.3 I want to display the quantities of all my products (simple and configurable products) in the product listing and in the product detail and in the product quickview. do you have a code to offer me? Thanks for your help

| improve this answer | |
  • Post this in the comment section or create new question. – Kowsigan Atsayam Jun 22 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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