I need to override


For this, in app/code/namespace/Customtab/view/frontend/layout/catalog_product_view.xml

<?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">

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

paste the custom phtml file in


But it didn't works.Please help me to find a solution.

2 Answers 2


Use this:

<?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">
        <referenceBlock name="product.info.media.image">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Namespace_Customtab::product/view/gallery.phtml</argument>
  • It works fine :)
    – Rita Jose
    May 30, 2017 at 7:32
  • when i add the above code the template gets overrided. but when you open configurable product and if you change the swatch it throws js error.
    – chris
    Sep 1, 2020 at 11:56

Customizing the core Magento 2 functionality is a bad coding practice. The solution is to override a phtml file using a custom module in Magento 2.

Learn the implementation of both the methods at Reference for override phtml

  • Please check the link if its available before posting
    – Tanmay
    Jul 27, 2020 at 10:49
  • @Tanmay link work Jul 28, 2020 at 8:39
  • 1
    It did not work previously may be site issuess
    – Tanmay
    Jul 28, 2020 at 9:06
  • yes may be possible Jul 28, 2020 at 9:22
  • Don't link of SE, just give an actual answer
    – Dom
    Jan 11, 2022 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.