5

I am trying to customize the product/view/addtocart.phtml template in my extension. I was already able to customize other templates but for this one it just won't work.

I found the template is defined in

vendor\magento\module-catalog\view\frontend\layout\catalog_product_view.xml 

among much more XML is this bit:

<block class="Magento\Catalog\Block\Product\View" name="product.info" template="product/view/form.phtml" after="alert.urls">
                <container name="product.info.form.content" as="product_info_form_content">
                    <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="product/view/addtocart.phtml"/>
                </container>

I know the XML which determines what template is loaded for the "product.info.addtocart". Because if I change the template of in this XML to template

MyNamespace_MyModule::product/view/addtocart.phtml 

It successfully overrides the template with my own. However of course I need to do this from within my module. I already tried many ways but not matter what I try it has no effect...

Among other things I have created a file

app\code\MyNamespace\MyModule\view\frontend\layout\catalog_product_view.xml 

and put the following code in it:

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<referenceBlock name="product.info.addtocart">
    <arguments>
        <argument name="template" xsi:type="array">
            <item name="0" xsi:type="string">MyNamespace_MyModule::product/view/addtocart.phtml</item>
        </argument>
    </arguments>
</referenceBlock>
<referenceContainer name="content" remove="true">
</layout>

and it has no effect. Interestingly notice the

<referenceContainer name="content" remove="true"> 

that should actually blank the page but it does nothing either (if I put the same XML bit in the Magento_Catalog catalog_product_view.xml then it effectively blanks the page..).

So I assume the problem is not that my code is not good but rather that my whole layout XML is not being read for some reason... I thought maybe I had a permission problem so just to be sure I chmoded the whole Module directory at 777 but still my layout XML has no effect...

How come I am unable to override anything, why is my

app\code\MyNamespace\MyModule\view\frontend\layout\catalog_product_view.xml 

having no effect?? Could it have something to do with the order of loading the modules?

If yes how come I am able to overide other layout XML from the Magento Catalog module from the same module such as

app\code\MyNamespace\MyModule\view\base\layout\catalog_product_prices.xml

Thanks for your help!!

  • Try to run php bin/magento setup:upgrade command, remove cache – Krishna ijjada Jun 23 '16 at 18:50
6

Well I was finally able to find an answer thanks to this very site:

I found my answer here: Magento-2 override template for Group product in my module

Basically to resume it turns out somehow my XML must not have been good. Inspired by one of the answers from the above link I changed my app/code/MyNamespace/MyModule/view/frontend/layout/catalog_product_view.xml code to

<?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>        
        <referenceContainer name="product.info.form.content">
            <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="MyNamespace_MyModule::product/view/addtocart.phtml"/>
        </referenceContainer>  
    </body>
</page>

and that worked!!!!

4

Steps to override the addtocart.phtml

1.Copy addtocart.phtml file from

vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml

And paste this file in your module

E.g.

app/code/CompanyName/Mymodule/view/frontend/templates/catalog/product/view/addtocart.phtml

2.Now Create catalog_product_view.xml file in your module app/code/CompanyName/Mymodule/view/frontend/layout/

<?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>
        <referenceContainer name="content">
            <referenceBlock name="product.info.addtocart">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">CompanyName_Mymodule::catalog/product/view/addtocart.phtml</argument>
                </action>
            </referenceBlock>
         </referenceContainer>
    </body>
</page>

3.Do whetever you want to change in addtocart.phtml

4.Run below Command Line on your Magento 2 root directory

php bin/magento cache:clean
2

Create catalog_product_view.xml file in your module app/code/Vendor/Module/view/frontend/layout/ and add below code

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

    <body>  
        <referenceBlock name="product.info.addtocart.additional">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Module::product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>                         
    </body>
</page>

Create addtocart.phtml file in your module app/code/Vendor/Module/view/frontend/templates/product/view and add your code here

<?php echo "addtocart.phtml file overite"  ?>

then flush cash using command or in admin cache management

php bin/magento c:f

same solution working for me

enter image description here

1

To override addtocart.phtml template file in your module use below code

  1. Create file catalog_product_view.xml in the directory path Vendor_Module/view/frontend/layout and add below 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>        
            <referenceContainer name="product.info.form.content">
                <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="Vendor_Module::catalog/product/view/addtocart.phtml"/>
            </referenceContainer>  
        </body>
    </page>
    
0

Follow the following steps-

  1. Create your module

  2. Copy file from

vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml

To

app/code/MyNamespace/MyModule/view/frontend/templates/catalog/product/view/addtocart.phtml

Make your change in this file

  1. Create layout file

app/code/Kh/CustomOption/view/frontend/layout/catalog_product_view.xml

code is given below

<?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">MyNamespace_MyModule::catalog/product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>

    </body>
</page>
  1. Clear the cache - bin/magento cache:clean

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.