0

It seems to be impossible to override the template vendor\magento\module-checkout\view\frontend\templates\cart\item\renderer\actions\edit.phtml from your own module e.g. by using layout updates.

(The template shows the edit button near quote items in the cart)

Preconditions:

Magento 2.3.5-p2

Steps to reproduce:

  1. Create own module (In my case: Company_Configurator)

  2. Create Template file: app\code\Company\Configurator\view\frontend\templates\cart\item\renderer\actions\edit.phtml and change content.

  3. Create layout update file app\code\Company\Configurator\view\frontend\layout\checkout_cart_item_renderers.xml with following content:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. 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.simple.actions.edit">
            <arguments>
                <argument name="template" xsi:type="string">Company_Configurator::cart/item/renderer/actions/edit.phtml</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Attempt 2:

I even tried to extend the block class, override it with dependency injection and then set the template:

app/code/Company/Configurator/etc/frontend/di.xml:

<?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\Actions\Edit"
                type="Company\Configurator\Block\Cart\Item\Renderer\Actions\Edit" />
</config>

app\code\Company\Configurator\Block\Cart\Item\Renderer\Actions\Edit.php

namespace Company\Configurator\Block\Cart\Item\Renderer\Actions;

class Edit extends \Magento\Checkout\Block\Cart\Item\Renderer\Actions\Edit
{
    protected $_template = 'Company_Configurator::cart/item/renderer/actions/edit.phtml';

    function __construct(\Magento\Framework\View\Element\Template\Context $context, array $data = [])
    {
        parent::__construct($context, $data);
    }

    ...

}

Expected result:

Template should get changed to own template.

Actual result:

Old template is still loading.

Please Help

1
+50

You can set template directly on <referenceBlock> in layout xml like this:

app\code\Company\Configurator\view\frontend\layout\checkout_cart_item_renderers.xml

<?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.simple.actions.edit" template="Company_Configurator::cart/item/renderer/actions/edit.phtml" />
</body>
</page>

Tested on Magento 2.3.5-p2

Another way to achieve this is to use <action method="setTemplate"> it is deprecated but has been for quite some time now and it's still used by Magento core code at various places.

You can also check this for additional info: Magento 2: Changing a Block's Template

  • I tried both methods, but none of them works. – Black Aug 10 '20 at 8:48
  • What product type do you have in cart? This example is for simple only. – Aleksa Živković Aug 10 '20 at 8:58
  • How do you know that it is only for simple products? The layout file is called checkout_cart_item_renderers.xml. We are using a custom product type named company_configurator – Black Aug 10 '20 at 9:12
  • 1
    Because of checkout.cart.item.renderers.simple.actions.edit block name and because you have those for each product type within magento, so configurable, bundle, virtual, grouped all exist. – Aleksa Živković Aug 10 '20 at 9:12
  • I see, thank you for the hint. We are using a custom product type though, which is called company_configurator – Black Aug 10 '20 at 9:12

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.