How to override .phtml files in Magento 2

  • I want to customize product details page, So it required to override some .phtml files in following locations.

    So I want to know, how to override .phtml files in vendor\magento\module-catalog\view\frontend\templates\product or vendor\magento\module-theme\view

  • Sometimes we need to make modifications to existing templates. Instead of making changes directly to existing template files, we should override them in our own theme. Let us assume that we want to update the category listing page ( list.phtml ). To do so, create following directory structure:

    app/design/frontend/Mycompany/Basic/Magento_Catalog/templates/product
    

    Here I assume that our current theme is Basic. Now copy the list.phtml file into product directory from the following location:

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

    Now you can make any modifications you want to your overridden file.

    For detail please read this tutorial it's help you lot.

    Hi, I just overrides the list.phtml file in the following path, \app\design\frontend\Magento\luma\Magento_Catalog\templates\product\list.phtml. But it hasn't worked. Can I know where I did wrong?

    Same problem, no matter how many times i hit refresh the default one is loaded over my custom one. And YES i have the correct path before anyone asks, but I am using composer to install magento so the defaults are in different locations if that matters.

    I had to clear the html block cache and then this worked perfectly for me! (I clear cache like this http://blog.gardenhouse.io/2018/05/29/maintaining-permissions-with-the-magento2-cli-tool/)

    How to override the template from your module though?

  • I am answering my own question,

    I found out how to override the .phtml files in the locations mentioned in my question,

    You have to put them in following locations to override the original files

    app\design\frontend\<VENDOR_NAME>\<THEME_NAME>\Magento_Catalog\templates\product\view
    

    And I believe the theory is same for overriding any other .phtml files in Magento 2

    Could you please brief more...Like i have to copy my custom phtml file into the theme folder isit? like into app\design\frontend\\luma\Magento_Catalog\ Should i have to create my Vendor folder and copy the core phtml file into it? If by doing so, no need of templates in view folder?

    Hi, I am trying to override a phtml file for a widget, and I'm not sure the path I have used is correct. Is the Magento_Catalog part of your path specific to the module folder you are overriding? So, if I am overriding the vendor\magento\module-catalog-widget\view\frontend\templates\product\widget\content, would I replace Magento_Catalog in your instance with Magento_Catalog_Widget in my instance? Slightly confused as it isn't clear to me yet.

  • To Override pthml and layout files from

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

    into

    app/code/Mycompany/OverideCatalog/view/frontend/templates/product/list.phtml
    

    Layout:

    vendor/magento/module-catalog/view/frontend/layout/catalog_category_view.xml
    

    into

    app/code/Mycompany/OverideCatalog/view/frontend/layout/catalog_category_view.xml
    
    <block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Mycompnay_OverideCatalog::product/list.phtml">
    

    This response is good for my needs but could have some more explanation.

  • To override phtml, layout and web files to own custom theme

    1) To Override template file:

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

    Follow this path

    app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/list.phtml

    2) To Override layout file:

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

    Follow this path

    app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_product_view.xml

    How to override/customize other module files like Observer, Controller etc?

  • override templates(phtml) by custom theme

    if you want to override vendor\magento\module-catalog\view\frontend\templates\product\some.phtml first you need to create your custom theme, please refer this link for how to create custom theme.

    after that create phtml file in your custom theme to override default one

     app/design/frontend/vendor-name/theme-name/Magento_Catalog/templates/product/some.phtml
    

    thats it finally clear the cache of magento as well as browser, then check your page, new phtml file only reflected.

    same way you can do for all templates override.

    good luck

    Thanks, I just figured it out and answered the finding in my own questions, your answer is much similar to my findings.

  • Open folder : /vendor/magento/theme-frontend-luma/ and copy the module templates and layout files from the view folder.

    Then create folders : /app/design/frontend/spacename/Theme/

    Then create module folder and then create templates and layout folder.

    Insert the templates and layout files from the module view folder.

  • Classy Llama did a good job on an article that shows multiple situations and how to override template files in each of them.

    https://www.classyllama.com/blog/template-override-m2

    It helped me when I came to this thread looking for answers.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM