Magento – Magento 2 : Override Template From “module-theme”

magento2overridestemplate

I want to override sections.phtml file from vendor/magento/module-theme/view/templates/html/

I am using default magento theme

I have created structure,

app/design/frontend/Magento/Magento-Theme/templates/html/sections.phtml

But it is not running, the default one is getting loaded.

Best Answer

You can achieve this two ways, choose either one as per your requirements [from module or theme]

From Theme

Create one custom theme after that you need to create the below file for override sections.phtml

app/design/frontend/YourVendorName/YourThemeName/Magento-Theme/templates/html/sections.phtml

From Module

Create custom module after that create below default.xml file to overwrite sections.phtml

app/code/YourVendorName/YourModuleName/view/frontend/layout/default.xml

    <?xml version="1.0"?>
    <!--
    /**
     * Copyright © 2013-2017 Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="navigation.sections">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">YourVendorName_YourModuleName::sections.phtml</argument>
                </action>
            </referenceBlock>
        </body>
    </page>     

place the sections.phtml in below path

app/code/YourVendorName/YourModuleName/view/frontend/templates/sections.phtml

Related Topic