1

How can I override addtocart.phtml for all types of products, this code works fine only for simple and configurable products, but I want to override all kinds of products.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="Eltrino_HideAddCart::css/disableaddtocart.css"/>
    </head>
    <body>
        <referenceBlock name="product.info.addtocart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string"> Module_Cart::catalog/product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string"> Module_Cart::catalog/product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>
0

Add to cart button in product page coming from Catalog module with product/view/addtocart.phtml template file. If you want to customization on addtocart template file in Magento 2, You can override addtocart.phtml file in your module by below way,

Create the catalog_product_view.xml file in your module, Path: app/code/{VendorName}/{PackageName}/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<!--
/**
 * 
 * @package VendorName_PackageName
 */
-->
<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">VendorName_PackageName::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">VendorName_PackageName::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

product.info.addtocart and product.info.addtocart.additional block contains the addtocart template file.

Now you have to create template file addtocart.phtml in your module at below location, app/code/{VendorName}/{PackageName}/view/frontend/templates/product/view/addtocart.phtml

copy the clone of template from Core Catalog module’s addtocart.phtml file and do your customization.

I hope this will help

  • I have already create all files, and add addtocart.phtml, but above code working only for configurable and simple products, but i want override bungle products for example – Mikel Aug 30 at 12:46

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.