1

How do I correct my code to fix this?

In my custom module I have the file app/code/MyCompany/MyModule/view/frontend/layout/checkout_cart_item_renderers.xml with the following code:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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">
            <block class="Magento\Checkout\Block\Cart\Item\Renderer" as="default" template="MyCompany_MyModule::cart/item/default.phtml">
                <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.default.actions" as="actions">
                    <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.default.actions.edit" template="Magento_Checkout::cart/item/renderer/actions/edit.phtml"/>
                    <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.default.actions.remove" template="Magento_Checkout::cart/item/renderer/actions/remove.phtml"/>
                </block>
            </block>
            <block class="Magento\Checkout\Block\Cart\Item\Renderer" as="simple" template="MyCompany_MyModule::cart/item/default.phtml">
                <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.simple.actions" as="actions">
                    <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.simple.actions.edit" template="Magento_Checkout::cart/item/renderer/actions/edit.phtml"/>
                    <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.simple.actions.remove" template="Magento_Checkout::cart/item/renderer/actions/remove.phtml"/>
                </block>
            </block>
        </referenceBlock>
    </body>
</page>

My custom template is in /app/code/MyCompany/MyModule/view/frontend/templates/cart/item/default.phtml.

I have tried removing the block first, then it doesn't load anything. Any help how to do this the right way is appreciated.

0

Add a new layout file named checkout_cart_item_renderers.xml in your layout folder at the following path:

app/code/Vendor/Module/view/frontend/layout/

Use the following code under body tag in this file:

    <referenceBlock name="checkout.cart.item.renderers">
        <block class="Magento\Checkout\Block\Cart\Item\Renderer" as="default" template="Vendor_Module::cart/item/default.phtml">
            <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" name="checkout.cart.item.renderers.default.actions" as="actions">
                <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.default.actions.edit" template="Magento_Checkout::cart/item/renderer/actions/edit.phtml"/>
                <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Remove" name="checkout.cart.item.renderers.default.actions.remove" template="Magento_Checkout::cart/item/renderer/actions/remove.phtml"/>
            </block>
        </block>
    </referenceBlock>

Using this code, you can override cart/item/default.phtml file in your module. Follow the same practice for other type of products.

| improve this answer | |
0

You need to override product specific referenceBlock to change the default template file as below in your custom checkout_cart_item_renderers.xml file :

<?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.bundle" template="MyCompany_MyModule::cart/item/default.phtml" />
        <referenceBlock name="checkout.cart.item.renderers.virtual" template="MyCompany_MyModule::cart/item/default.phtml" />
        <referenceBlock name="checkout.cart.item.renderers.default" template="MyCompany_MyModule::cart/item/default.phtml" />
        <referenceBlock name="checkout.cart.item.renderers.simple" template="MyCompany_MyModule::cart/item/default.phtml" />
        <referenceBlock name="checkout.cart.item.renderers.configurable" template="MyCompany_MyModule::cart/item/default.phtml" />
        <referenceBlock name="checkout.cart.item.renderers.grouped" template="MyCompany_MyModule::cart/item/default.phtml" />
    </body>
</page>
| improve this answer | |

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.