1

I need to add new button near edit button on cart page. I want o override edit.phtml in my custom module.
Can someone guide me how to achieve this?

vendor/magento/module-checkout/view/frontend/templates/cart/item/renderer/actions/edit.phtml

1

If you want to override the template then you can use plugin

Create after Plugin getTemplate method on the class Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit

and use your template file as the alternative of default.phtml

Create di.xml at app/code/{Vendorname}/{Modulename}/etc/frontend/ for define the plugin class:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit">
      <plugin name="change_template_for_edit" type="{Vendorname}\{Modulename}\Plugin\Cart\EditPlugin" sortOrder="1" disabled="false" />
    </type>
</config>

And finally, add the plugin class EditPlugin.php at app/code/{Vendorname}/{Modulename}/Plugin/Cart

<?php
namespace {Vendorname}\{Modulename}\Plugin\Cart;
class EditPlugin
{

       public function afterGetTemplate(
    \Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit $subject,
     $result
) { // use Own module edit.phtml
    return '{Vendorname}_{Modulename}::cart/item/renderer/actions/edit.phtml';
}

}

Create a template file which is replicate of default edit.phtml at app/code/{Vendorname}/{Modulename}//view/frontend/templates/cart/item/renderer/actions/edit.phtml

  • PHP Parse error: syntax error, unexpected ',', expecting variable (T_VARIABLE) in /Plugin/Cart/EditPlugin.php on line 7 – Arjun Feb 7 at 9:05
  • Fixed, the syntax issue – Amit Bera Feb 7 at 9:21
0

Copy edit.phtml from core file

vendor\magento\module-checkout\view\frontend\templates\cart\item\renderer\actions

Keep edit.phtml file in below path

app\design\frontend\Your_Vendor\Your_Theme\Magento_Checkout\templates\cart\item\renderer\actions

Do your changes and clear the cache and check.

  • This is working fine but if i want it to be done via app/code/cutom module then how can these be achieved. – Arjun Feb 7 at 6:41
  • I think, you dont need to create a custom module for this file it is just a default magento file so you can override in the theme as i suggested. – Mohammad Faizan Feb 7 at 6:47
  • You are right @Rakesh, we can do as you suggested, but i am suggesting to Arjun that there is no need to create custom module because this is just a simple file – Mohammad Faizan Feb 7 at 7:31
0

You need to override in your module like below.

First copy an edit.phtml file in your module like

app/code/Mycompany/MyModule/view/frontend/templates/cart/item/renderer/actions/edit.phtml

Add Button code in this file.

After that create a file

app/code/Mycompany/MyModule/view/frontend/layout/checkout_cart_index.xml

And add the following code.

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

Then create this file

app/code/Mycompany/MyModule/view/frontend/layout/checkout_cart_item_renderers.xml

And add the following code

<?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="Magento_Checkout::cart/item/default.phtml">
            <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" as="actions">
                <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.default.actions.edit" template="Mycompany_MyModule::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="Magento_Checkout::cart/item/default.phtml">
            <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions" as="actions">
                <block class="Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit" name="checkout.cart.item.renderers.default.actions.edit" template="Mycompany_MyModule::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>
</body>

Note: Please check Mycompany_MyModule in above file to override edit.phtml file.

Clear cache and check, Hope it will work for you.

  • I have tried the code but doesn't work as expected. – Arjun Feb 7 at 10:17
0
  1. Please Copy the file in

vendor/magento/module-checkout/view/frontend/templates/cart/item/renderer/actions/edit.phtml

  1. Go to app/design/frontend/Your_Namespace/Your_Theme/Magento_Checkout/templates/cart/item/renderer/actions

  2. Paste the copied file edit.phtml into your theme

  • This is working fine but if i want it to be done via app/code/cutom module then how can these be achieved. – Arjun Feb 7 at 6:41

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.