I have a selection of configurable products, mainly all color-related. I want to be able to add custom code in the cart showing the stock levels on each child.

The example below has 2 parts added to the cart - one in black which has 9 in stock in the backend and white which has 0 in stock - but when I run the code it shows me 9 for both instances.

How can I get 2 different figures?

The Cart

Below is a snippet of the code which displays the numbers Code Snippet

2 Answers 2


In your custom module please create this file under view/frontend/layout/checkout_cart_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        <!-- override checkout cart item template -->
        <referenceBlock name="checkout.cart.form">
            <action method="setOverriddenTemplates">
                <argument name="template" xsi:type="array">
                    <!-- list override templates -->
                    <item name="default" xsi:type="string">vendor_module::cart/item/default.phtml</item>
                    <item name="simple" xsi:type="string">vendor_module::cart/item/default.phtml</item>
                    <item name="virtual" xsi:type="string">vendor_module::cart/item/default.phtml</item>

        <referenceBlock class="elastron\elcustom\Block\Cart\Grid" name="checkout.cart.form" template="elastron_elcustom::cart/form.phtml">
            <block class="Magento\Framework\View\Element\RendererList" name="checkout.cart.item.renderers" as="renderer.list"/>
            <block class="Magento\Framework\View\Element\Text\ListText" name="checkout.cart.order.actions"/>

Now under view/frontend/templates/cart/item/default.phtml

$_item = $block->getItem();
$product = $_item->getProduct();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($product->getId());

$isVisibleProduct = $product->isVisibleInSiteVisibility();
/** @var \Magento\Msrp\Helper\Data $helper */
$helper = $this->helper('Magento\Msrp\Helper\Data');
$canApplyMsrp = $helper->isShowBeforeOrderConfirm($product) && $helper->isMinimalPriceLessMsrp($product);

<tbody class="cart item">
    <tr class="item-info">
        <td data-th="<?= $block->escapeHtml(__('Item')) ?>" class="col item">
            <?php if ($block->hasProductUrl()):?>
                <a href="<?= /* @escapeNotVerified */ $block->getProductUrl() ?>"
                   title="<?= $block->escapeHtml($block->getProductName()) ?>"
            <?php else:?>
                <span class="product-item-photo">
            <?php endif;?>
            <?= $block->getImage($block->getProductForThumbnail(), 'cart_page_product_thumbnail')->toHtml() ?>
            <?php if ($block->hasProductUrl()):?>
            <?php else: ?>
            <?php endif; ?>
            <div class="product-item-details">
                <strong class="product-item-name">
                    <?php if ($block->hasProductUrl()):?>
                        <a href="<?= /* @escapeNotVerified */ $block->getProductUrl() ?>"><?= $block->escapeHtml($block->getProductName()) ?></a>
                    <?php else: ?>
                        <?= $block->escapeHtml($block->getProductName()) ?>
                    <?php endif; ?>
                <?= "YOUR MESSAGE HERE" ?>
                <?php if ($_options = $block->getOptionList()):?>
                    <dl class="item-options">
                        <?php foreach ($_options as $_option) : ?>
                            <?php $_formatedOptionValue = $block->getFormatedOptionValue($_option) ?>
                            <dt><?= __($block->escapeHtml($_option['label'])) ?></dt>
                                <?php if (isset($_formatedOptionValue['full_view'])): ?>
                                    <?= /* @escapeNotVerified */ $_formatedOptionValue['full_view'] ?>
                                <?php else: ?>
                                    <?= __($block->escapeHtml($_formatedOptionValue['value'], ['span'])) ?>
                                <?php endif; ?>
                        <?php endforeach; ?>
                <?php endif;?>
                <?php if ($messages = $block->getMessages()): ?>
                    <?php foreach ($messages as $message): ?>                        
                        <div class="cart item message <?= /* @escapeNotVerified */ $message['type'] ?>"><div><?= $block->escapeHtml($message['text']) ?></div></div>
                    <?php endforeach; ?>
                <?php endif; ?>
                <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
                <?php if ($addInfoBlock): ?>
                    <?= $addInfoBlock->setItem($_item)->toHtml() ?>
                <?php endif;?>

        <?php if ($canApplyMsrp): ?>
            <td class="col msrp" data-th="<?= $block->escapeHtml(__('Price')) ?>">
                <span class="pricing msrp">
                    <span class="msrp notice"><?= /* @escapeNotVerified */ __('See price before order confirmation.') ?></span>
                    <?php $helpLinkId = 'cart-msrp-help-' . $_item->getId(); ?>
                    <a href="#" class="action help map" id="<?= /* @escapeNotVerified */ ($helpLinkId) ?>" data-mage-init='{"addToCart":{"helpLinkId": "#<?= /* @escapeNotVerified */ $helpLinkId ?>","productName": "<?= /* @escapeNotVerified */ $product->getName() ?>","showAddToCart": false}}'>
                        <span><?= /* @escapeNotVerified */ __("What's this?") ?></span>
        <?php else: ?>
            <td class="col price" data-th="<?= $block->escapeHtml(__('Price')) ?>">
                <?= $block->getUnitPriceHtml($_item) ?>
        <?php endif; ?>
        <td class="col qty" data-th="<?= $block->escapeHtml(__('Qty'))  ?>">
            <div class="field qty">
                <div class="control qty">
                    <label for="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty">
                        <span class="label"><?= /* @escapeNotVerified */ __('Qty') ?></span>
                        <input id="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty"
                               name="cart[<?= /* @escapeNotVerified */ $_item->getId() ?>][qty]"
                               data-cart-item-id="<?= $block->escapeHtml($_item->getSku()) ?>"
                               value="<?= /* @escapeNotVerified */ $block->getQty() ?>"
                               title="<?= $block->escapeHtml(__('Qty')) ?>"
                               class="input-text qty"
                    <div ><?= $unit ?></div>

        <td class="col subtotal" data-th="<?= $block->escapeHtml(__('Subtotal')) ?>">
            <?php if ($canApplyMsrp): ?>
                <span class="cart msrp subtotal">--</span>
            <?php else: ?>
                <?= $block->getRowTotalHtml($_item) ?>
            <?php endif; ?>
    <tr class="item-actions">
        <td colspan="4">
            <div class="actions-toolbar">
                <?= /* @escapeNotVerified */ $block->getActions($_item) ?>

If this solved your issue please don't forget to up vote / mark as answered. Thanks !


While this generates a message on the cart, the goal is to make it so if a child product of the configurable product has 0 stock a message appears stating we have no stock, that is the main goal, so I am trying to work out how to extract the simple product QTY from the configurable one

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.