Magento2 – Change Add to Cart Template on Product View

addtocartlayoutmagento2

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.

Best Answer

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" />
Related Topic