8

I want to override :

/opt/lampp/htdocs/magento_composer/vendor/magento/module-catalog/view/frontend/templates/product/view/addtocart.phtml

i have copied and pasted it in my custom module at :

/opt/lampp/htdocs/magento_composer/app/code/Hello/Custom/view/frontend/templates/product/view/addtocart.phtml

This is my Layout file:

<?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" template="Hello_Custom::product/view/addtocart.phtml" after="alert.urls">

        </referenceBlock>

    </body>
</page>

at:

/opt/lampp/htdocs/magento_composer/app/code/Hello/Custom/view/frontend/layout/catalog_product_view.xml

module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Hello_Custom" setup_version="1.0.0" schema_version="1.0.0">
    </module>
</config>
  • Module is working for simple hello world so its registered correctly ...

The issue is in template path hints file comes from vendor.. but it should come from my custom module only (not from theme)

  • is catalog_product_view.xml naming is essential or we can give any name to file. actually I need help on overriding template – siddhesh Jul 13 '17 at 13:18
  • siddhesh the naming of that file is definitely essential. It determines where the layout file is loaded. For example default.xml is loaded on every page but catalog_product_view.xml is only loaded on the catalog/product/view action. – Jacques Oct 27 '17 at 12:25
17

you can try with below code in xml file,

<?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>
        <referenceContainer name="content">
            <referenceBlock name="product.info.addtocart">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Hello_Custom::product/view/addtocart.phtml</argument>
                </action>
            </referenceBlock>
         </referenceContainer>
    </body>
</page>
| improve this answer | |
  • Yes definitely .. – Narendra.vyas Sep 6 '16 at 9:02
  • how to check if layout xml file is used or not? i tried to write invalid xml and still my page was rendered correctly . so I concluded my xml layout file is not executed do you tell me how to solve this – siddhesh Jul 13 '17 at 15:09
  • or do we need to run some kind of commands after updating xml file – siddhesh Jul 13 '17 at 15:10
  • 1
    Actually, in the dev docs of magento 2 you can read "using the <action> instruction. This way is not recommended, but can be used for... ". I would recommend using the method suggested on that page under Set a block template. So using the action to change the template is not a good solution. – drew7721 Oct 27 '17 at 16:19
10

There are three methods available to override template files in Magento

For example: Here we override addtocart.phtml file for simple product.

Method 1:

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

Method 2:

<referenceContainer name="content">
    <referenceBlock name="product.info.addtocart" template="Vendor_Module::product/view/addtocart.phtml"/>
</referenceContainer>

Method 3:

<referenceContainer name="product.info.form.content">
    <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="Vendor_Module::product/view/addtocart.phtml"/>
</referenceContainer>
| improve this answer | |

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.