7

I want to customize the cart page. I'm trying to override it using module. I created module which is working fine. I created layout:

checkout_cart_item_renderers.xml

<?xml version="1.0"?> <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.cart.item.renderers">
            <referenceBlock name="default">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Custom_Addprofile::cart/item/default.phtml</argument>
                </action>
            </referenceBlock>
         </referenceBlock>
    </body> </page>

I created the template file as:

templates/cart/item/default.phtml

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Checkout\Block\Cart\Item\Renderer */
die('ssssssssssssssssssssss');
$_item = $block->getItem();
$product = $_item->getProduct();
$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="<?php echo $block->escapeHtml(__('Item')); ?>" class="col item">
            <?php if ($block->hasProductUrl()):?>
                <a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl() ?>"
                   title="<?php echo $block->escapeHtml($block->getProductName()) ?>"
                   tabindex="-1"
                   class="product-item-photo">
            <?php else:?>
                <span class="product-item-photo">
            <?php endif;?>
            <?php echo $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="<?php /* @escapeNotVerified */ echo $block->getProductUrl() ?>"><?php echo $block->escapeHtml($block->getProductName()) ?></a>
                    <?php else: ?>
                        <?php echo $block->escapeHtml($block->getProductName()) ?>
                    <?php endif; ?>
                </strong>
                <?php if ($_options = $block->getOptionList()):?>
                    <dl class="item-options">
                        <?php foreach ($_options as $_option) : ?>
                            <?php $_formatedOptionValue = $block->getFormatedOptionValue($_option) ?>
                            <dt><?php echo $block->escapeHtml($_option['label']) ?></dt>
                            <dd>
                                <?php if (isset($_formatedOptionValue['full_view'])): ?>
                                    <?php /* @escapeNotVerified */ echo $_formatedOptionValue['full_view'] ?>
                                <?php else: ?>
                                    <?php /* @escapeNotVerified */ echo $_formatedOptionValue['value'] ?>
                                <?php endif; ?>
                            </dd>
                        <?php endforeach; ?>
                    </dl>
                <?php endif;?>
                <?php if ($messages = $block->getMessages()): ?>
                    <?php foreach ($messages as $message): ?>
                        <div class="cart item message <?php /* @escapeNotVerified */ echo $message['type'] ?>"><div><?php echo $block->escapeHtml($message['text']) ?></div></div>
                    <?php endforeach; ?>
                <?php endif; ?>
                <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
                <?php if ($addInfoBlock): ?>
                    <?php echo $addInfoBlock->setItem($_item)->toHtml() ?>
                <?php endif;?>
            </div>
        </td>

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

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

I cannot get it done. When loading

localhost/magentostore/checkout/cart/ page. It loads the cart page from magento core which I verified by putting die() in default.phtml temporarily.

1
  • hi anime how to get product id instead of product name. actually i am getting parent product id instead of child product id Jul 21, 2017 at 13:20

4 Answers 4

8

you can do it using override in your module with below way,

You must have to change referencename of your xml,

First create app/code/Custom/Addprofile/view/frontend/layout/checkout_cart_index.xml,

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock class="Magento\Checkout\Block\Cart" name="checkout.cart.form">
            <block class="Magento\Framework\View\Element\RendererList" name="checkout.cart.item.renderers.override" as="renderer.list.custom"/>
            <arguments>
                <argument name="renderer_list_name" xsi:type="string">checkout.cart.item.renderers.override</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Now your checkout_cart_item_renderers.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>
        <referenceBlock name="checkout.cart.item.renderers.override">
            <block class="Magento\Checkout\Block\Cart\Item\Renderer" as="default" template="Custom_Addprofile::cart/item/default.phtml" />              

            <block class="Magento\Checkout\Block\Cart\Item\Renderer" as="simple" template="Custom_Addprofile::cart/item/default.phtml" />  
        </referenceBlock>
    </body>
</page>
6
  • Hi Rakesh, i want to get product id instead of product name. in same page who are in cart Jul 21, 2017 at 13:21
  • Rakesh! Thank you for this! You are a hero. Nov 7, 2017 at 18:13
  • @TonyDeStefano Thanks for your kind words! Glad to know you have useful answer. Nov 8, 2017 at 4:51
  • I tried it does not work, referenceBlock does not have class name. Feb 1, 2018 at 11:53
  • remove class from <referenceBlock class="Magento\Checkout\Block\Cart" Mar 8, 2018 at 4:57
5

Finally after three days. I got it working. Below is the correct way of overriding it.

app\code\Vendor\Module\Block\Cart\AbstractCart.php

<?php

/**
 * Override cart item block in custom module
 */

namespace Vendor\Custom\Block\Cart;

class AbstractCart
{

    public function afterGetItemRenderer(\Magento\Checkout\Block\Cart\AbstractCart $subject, $result)
    {
        $result->setTemplate('Vendor_Custom::cart/item/default.phtml');
        return $result;
    }
}

app\code\Vendor\Module\etc\di.xml

<!--To override cart template--> 
    <type name="Magento\Checkout\Block\Cart\AbstractCart">
        <plugin name="cart-item-override" type="Vendor\Custom\Block\Cart\AbstractCart" sortOrder="1"/>
    </type>

app\code\Vendor\Custom\view\frontend\cart\item\default.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Checkout\Block\Cart\Item\Renderer */
die('template overridden');
$_item = $block->getItem();
$product = $_item->getProduct();
$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>
                <p><strong>Note: </strong>This is a renewal prodcut.</p>
                <?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: ?>
                                    <?= /* @escapeNotVerified */ $_formatedOptionValue['value'] ?>
                                <?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">
                <label class="label" for="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty">
                    <span><?= /* @escapeNotVerified */ __('Qty') ?></span>
                </label>
                <div class="control qty">
                    <input id="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty"
                           name="cart[<?= /* @escapeNotVerified */ $_item->getId() ?>][qty]"
                           data-cart-item-id="<?= /* @escapeNotVerified */ $_item->getSku() ?>"
                           value="<?= /* @escapeNotVerified */ $block->getQty() ?>"
                           type="number"
                           size="4"
                           title="<?= $block->escapeHtml(__('Qty')) ?>"
                           class="input-text qty"
                           data-validate="{required:true,'validate-greater-than-zero':true}"
                           data-role="cart-item-qty"/>
                </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="100">
            <div class="actions-toolbar">
                <?= /* @escapeNotVerified */ $block->getActions($_item) ?>
            </div>
        </td>
    </tr>
</tbody>

Thanks

1
4

Here is the correct way to do an override the default.phtml.

First, you need to write sequence in module.xml. Without these sequences, you can't override.

app/code/VendorName/ModuleName/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="VendorName_ModuleName" setup_version="0.0.1">
    <sequence>
        <module name="Magento_Checkout" />
        <module name="Magento_Bundle" />
        <module name="Magento_Catalog" />
        <module name="Magento_ConfigurableProduct" />
        <module name="Magento_Downloadable" />
        <module name="Magento_GroupedProduct" />
    </sequence>
</module>

On the next step you should set your template in checkout_cart_item_renderers.xml

app/code/VendorName/ModuleName/view/frontend/layout/checkout_cart_item_renderers.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>

    <referenceBlock name="checkout.cart.item.renderers.bundle" template="VendorName_ModuleName::cart/item/default.phtml" />
    <referenceBlock name="checkout.cart.item.renderers.virtual" template="VendorName_ModuleName::cart/item/default.phtml" />
    <referenceBlock name="checkout.cart.item.renderers.default" template="VendorName_ModuleName::cart/item/default.phtml" />
    <referenceBlock name="checkout.cart.item.renderers.simple" template="VendorName_ModuleName::cart/item/default.phtml" />
    <referenceBlock name="checkout.cart.item.renderers.configurable" template="VendorName_ModuleName::cart/item/default.phtml" />
    <referenceBlock name="checkout.cart.item.renderers.grouped" template="VendorName_ModuleName::cart/item/default.phtml" />

</body>
</page>

And last step it is your template which should be in this path.

app/code/VendorName/ModuleName/view/frontend/templates/cart/item/default.phtml

Enjoy!

1

I found an answer using plugin way here

How to use rewrite the cart/item/default.phtml template file to my module in Magento2

Which lets us override template. But cannot we do this in the way I did? If there is any mistake then please point.

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.