0

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

0

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" 
    xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <!-- 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>
                </argument>
            </action>
        </referenceBlock>


        <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"/>
        </referenceBlock>
    </body>
</page>

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

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

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

$unit=$_product->getData("unit");
$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()) ?>"
                   tabindex="-1"
                   class="product-item-photo">
            <?php else:?>
                <span class="product-item-photo">
            <?php endif;?>
            <?= $block->getImage($block->getProductForThumbnail(), 'cart_page_product_thumbnail')->toHtml() ?>
            <?php if ($block->hasProductUrl()):?>
                </a>
            <?php else: ?>
                </span>
            <?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; ?>
                </strong>
                <?= "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>
                            <dd>
                                <?php if (isset($_formatedOptionValue['full_view'])): ?>
                                    <?= /* @escapeNotVerified */ $_formatedOptionValue['full_view'] ?>
                                <?php else: ?>
                                    <?= __($block->escapeHtml($_formatedOptionValue['value'], ['span'])) ?>
                                <?php endif; ?>
                            </dd>
                        <?php endforeach; ?>
                    </dl>
                <?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;?>
            </div>
        </td>

        <?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>
                    </a>
                </span>
            </td>
        <?php else: ?>
            <td class="col price" data-th="<?= $block->escapeHtml(__('Price')) ?>">
                <?= $block->getUnitPriceHtml($_item) ?>
            </td>
        <?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() ?>"
                               type="number"
                               size="5"
                               title="<?= $block->escapeHtml(__('Qty')) ?>"
                               class="input-text qty"
                               data-validate="{required:true,'validate-greater-than-zero':true}"
                               data-role="cart-item-qty"/>
                    </label>
                    <div ><?= $unit ?></div>

                </div>
            </div>
        </td>
        <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; ?>
        </td>
    </tr>
    <tr class="item-actions">
        <td colspan="4">
            <div class="actions-toolbar">
                <?= /* @escapeNotVerified */ $block->getActions($_item) ?>
            </div>
        </td>
    </tr>
</tbody>

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

0

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.