I have got it working and its outputting the right information, but I do not get the toggle to work. Now I think thats because of the use of swatches. Hopefully someone here can help me out with that!

Original answer: https://magento.stackexchange.com/a/208985/72996

If I edit configurable.js the way I think needed its not working, but I can't find the output in the source either, so I think swatch-renderer.js is used, but then I don't know how and where to place it.


1 Answer 1


Try below solution for configurable swatches

Step1: app/design/frontend/Vendor/Theme/Magento_Swatches/templates/product/view/renderer.phtml

Use code as in original question

    $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"><?php
                $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($child->getId());
                    echo "We have ".round($productStockObj->getData('qty'))." items in stock!";?>

    } else {
        $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($product->getId());
        echo "We have ".round($productStockObj->getData('qty'))." items in stock!";

Step2: app/design/frontend/Vendor/Theme/Magento_Swatches/web/js/swatch-renderer.js

in _EventListener function add below code


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.