1

I am using a slightly modified Luma theme and want to differ the text on the Add to cart button for product pages only. For this I have created another addtocart.phtml called addtocartmain.phtml which I want to use as the template for add to cart block for product pages. I have then attempted to override the default template using the below code within vendor/theme/Magento_Catalog/layout/catalog_product_view.xml to the body node.

<referenceBlock name="product.info.addtocart">
        <action method='setTemplate'>
            <argument name="template" xsi:type="string">Magento_Catalog::product/view/addtocartmain.phtml</argument>
        </action>
</referenceBlock> 

The addtocartmain.phtml is located within:

'vendor/theme/Magento_Catalog/templates/product/view' 

After hours or attempting multiple methods of changing the template this is not changing the add to cart button. What could be stopping this from working? I can change the referenceBlock to any other block name and override them just fine with my new template. I have even removed all modules, removed everything but this one modification and tried making these modifications within the core vendor files and still this block is not changing.

1
  • 1
    FYI the action tag has been deprecated, <arguments> should be used instead.
    – Ben Crook
    Jun 2, 2017 at 15:36

2 Answers 2

3

After tinkering around for a while I noticed that there was another block that was using the addtocart.phtml in the core:

'product.info.addtocart.additional' 

Overriding this block and the original 'product.info.addtocart' block was they key to switching out my add to cart button with a new button on all pages. Add the below code to catalog_product_view.xml within the body node as suggested by thaddeusmt and Ben Crook.

<referenceBlock name="product.info.addtocart" template="Magento_Catalog::product/view/addtocartmain.phtml" />
<referenceBlock name="product.info.addtocart.additional" template="Magento_Catalog::product/view/addtocartmain.phtml" />
2

You may need to specify which module or theme directory the template is in. For instance, if it's in the MyCompany_mytheme theme, in the Magento_Catalog template override directory:

app/design/frontend/MyCompany/mytheme/Magento_Catalog/templates/product/view/addtocartmain.phtml

Try adding Magento_Catalog:: to the path in catalog_product_view.xml:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
        <referenceBlock name="product.info.addtocart">
            <action method='setTemplate'>
                <argument name="template" xsi:type="string">Magento_Catalog::product/view/addtocartmain.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

As Ben Crook noted in a comment though, <action> is being deprecated, so this is the correct way to do it now:

<referenceBlock name="product.info.addtocart" template="Magento_Catalog::product/view/addtocartmain.phtml" />

That's assuming you are overriding the catalog_product_view.xml layout file correctly, of course. It should be here in your theme (if you are using a theme, not a module):

app/design/frontend/MyCompany/mytheme/Magento_Catalog/layout/catalog_product_view.xml

Be sure to clean the layout cache to check if it's working:

$ bin/magento cache:clean layout
7
  • Yeah i have it in correct place within theme and other modifications are working. I have tried adding Magento_Catalog however with no success.
    – harri
    Jun 2, 2017 at 15:35
  • I added one more suggestion: try using the "product.info.addtocart" block's addtocart alias in the <referenceBlock>
    – thaddeusmt
    Jun 2, 2017 at 15:42
  • That is not working either
    – harri
    Jun 2, 2017 at 15:52
  • I just removed the suggestion about the addtocart alias, it didn't work. I also added the complete catalog_product_view.xml file. But I just tested the code above, and it works. Be sure to check the full path location of the catalog_product_view.xml file and addtocartmain.phtml. And remember, if this is a module, not a theme, the path will be different. Is this a module or a theme?
    – thaddeusmt
    Jun 2, 2017 at 16:40
  • This is just in a theme. Path to layout is exactly as you have described and so is the template. catalog_product_view.xml is also exactly like yours other than a few extra rules.
    – harri
    Jun 2, 2017 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.