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!!
Best Answer
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 toand that worked!!!!