Question

Sorry but I dont know how to define my quetion.Let me show you code :

Magento 1 :

 <checkout_cart_index>
    <reference name="checkout.cart">
        <action method="addItemRender">
            <type>simple</type>
            <block>mymodule/checkout_cart_item_renderer_simple</block>
            <template>checkout/cart/item/default.phtml</template>
        </action>
     </reference>
</checkout_cart_index>

Above code is for override simple product entry in cart page.

Now my query is how can I achieve same thing in Magento 2.

Thank you.

Was it helpful?

Solution

In Magento, the renderers are defined under checkout_cart_item_renderers.xml, there's several of them:

  • app/code/Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml for default and simple products renderers
  • app/code/Magento/Bundle/view/frontend/layout/checkout_cart_item_renderers.xml for bundle products renderers
  • app/code/Magento/Catalog/view/frontend/layout/checkout_cart_item_renderers.xml for virtual products renderers
  • app/code/Magento/ConfigurableProduct/view/frontend/layout/checkout_cart_item_renderers.xml for configurable products renderers
  • app/code/Magento/Grouped/view/frontend/layout/checkout_cart_item_renderers.xml for grouped products renderers
  • app/code/Magento/GiftMessage/view/frontend/layout/checkout_cart_item_renderers.xml this one is special as it adds extra block for the gift message feature to the different renderers
  • app/code/Magento/Wishlist/view/frontend/layout/checkout_cart_item_renderers.xml this one is special too as it adds extra block for the wishlist feature to the different renderers

After checking those renderers, you can find which renderer you want to override.

As you said, you want to override the simple product renderer which is a Magento\Checkout\Block\Cart\Item\Renderer.

If you want to override the entire block you should use the Magento 2 preferences:

In your module, you can create a di.xml file under the etc folder with the following content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Cart\Item\Renderer" type="Vendor\ModuleName\Block\Checkout\Cart\Item\Renderer" />  
</config>

Then you need to create the app/code/Vendor/Module/Block/Checkout/Cart/Item/Renderer.php class with the following content:

<?php
namespace Vendor\Module\Block\Checkout\Cart\Item;
class Renderer extends \Magento\Checkout\Block\Cart\Item\Renderer
{
    public function theFunctionYouWantToRewrite()
    {
        // Do your custom stuff
    }
}

However, if you want to work on the method levels and not on the entire class level, you should use plugins

As it can be very specific, it's hard for me to give you an example but you can find useful resources about plugins here:

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top