6

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.

2

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:

  • Thanks for sharing useful knowledge. Actually I need to override renderer block for all product types.And so I am preferring same code which I am using in magento 1. So can you help me regarding that ? May I need to override all renderer xml files for respective product type ? – Krupali Apr 15 '16 at 11:32
  • @wiiliam well the code you provided only overwrites the block for simple products. If you need to rewrite the renderer for each product type then you'll have to follow the procedure I gave you for every block type (using the list of files I gave at the begginning of my answer). – Raphael at Digital Pianism Apr 15 '16 at 11:35
  • It helps me.If I take and change in file app/code/Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml then this will affect on default and simple product.But how can i do for all product types.Because at the end all file names are same. :( – Krupali Apr 15 '16 at 12:00
  • @wiiliam do not change directly in the files I provided as your changes will be overridden when you upgrade Magento. It is very bad practice to override core files, please follow my answer and use either preferences or plugins to make your modifications – Raphael at Digital Pianism Apr 15 '16 at 15:40

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.