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
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
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.
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.
vendor/magento/module-checkout/view/frontend/templates/cart/item/renderer/actions/edit.phtml
Go to app/design/frontend/Your_Namespace/Your_Theme/Magento_Checkout/templates/cart/item/renderer/actions
Paste the copied file edit.phtml into your theme