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.
Methods to Override a phtml file using a custom module in Magento 2:
1. Create registration.php file in app\code\[Webiators]\[HelloMagento]\
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, '[Webiators]_[HelloMagento]', __DIR__ ); |
2. Create module.xml file in app\code\[ Webiators ]\[HelloMagento]\etc
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="[Webiators]_[HelloMagento]" setup_version="1.0.0"/> </config> |
Method 1: Override a phtml file using a custom module in Magento 2 with plugin
In this method, we use the method to create plugin in Magento 2
1 |
1. Create di.xml file in app\code\[Webiators]\[HelloMagento]\etc |
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Block\Product\View\Type\Simple"> <plugin name="custom-template" type="[Webiators]\[HelloMagento]\Plugin\Block\Product\View\Type\Simple" /> </type> </config> |
1 |
2. Create Simple.php file in app\code\[Webiators]\[HelloMagento]\Plugin\Block\Product\View\Type |
1 2 3 4 5 6 7 8 9 10 |
<?php namespace [Webiators]\[HelloMagento]\Plugin\Catalog\Block; class Simple { public function beforeToHtml(\Magento\Catalog\Block\Product\View\Type\Simple $block) { $block->setTemplate('[Webiators]_[HelloMagento]::product/view/type/default.phtml'); } } |
Method 2: Override a phtml file using a custom module in Magento 2 with xml
1 |
Create catalog_product_view_type_simple.xml file in app\code\[Webiators]\[HelloMagento]\view\frontend\layout |
1 2 3 4 5 6 7 8 9 10 |
<?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"> <body> <referenceBlock name="product.info.simple"> <action method="setTemplate"> <argument name="template" xsi:type="string">[Webiators]_[HelloMagento]::product/view/type/default.phtml</argument> </action> </referenceBlock> </body> </page> |
The steps I mention above is the shortest process for you to Override a Template File in Magento 2. With this guide, you can manage the Template File in Magento 2 easily. Every store has a Template File in Magento 2 with many attributes.
Thank you !