How To Override Phtml File In Magento2

Blog
Spread the love

Before knowing how to override a Phtml template file in a custom module in Magento 2, we must focus on what is a Phtml template file?

What is a Phtml Template File?

An html code with php script that is parsed by the PHP Engine on web server, is called a phtml file. It is generally used to retrieve and access data to databases like MySQL, Postgres..etc. Simply can say An HTML page including a PHP script is known as a phtml template.

Overriding a file:

Overriding means rewriting functionalities in your own fashion without modifying the Magento core files.

Override a phtml file in a custom module:

As we know, Magento 2 is a rich E-commerce Platform that enables you to add your own functionalities to websites, but modifying the core functionality of Magento 2 is really not a good coding practice. whenever you upgrade your Magento version this customization will not affect your code and you will feel helpless.

Hence, it is strongly recommended better you override a function, class, or file, as per the requirements and desires.

There are two ways to override the phtml file in Magento2

1. Override a phtml file in a theme in Magento 2

2. Override a phtml file in a custom module in Magento 2

Here we will discuss second way to override a phtml file in a custom module in Magento 2.

Step 1: Create file registration.php at app/code/Vendor/Module/ folder (In my case vendor is Bluethink, Module is Overridetemplate and my file path is: app/code/Bluethink/Overridetemplate/registration.php)

Step 2: Create module.xml file at app/code/Vendor/Module/etc/ folder (In my case vendor is Bluethink, Module is Overridetemplate and my file path is : app/code/Bluethink/Overridetemplate/etc/module.xml)

Step 3: Create file catalog_category_view.xml at app/code/Vendor/Module/ view/frontend/layout/ folder

Now, you need to create a XML file under view/frontend/layout folder, In which you need to declare original name of referenceBlock where you will redefine path of your custom template.

(In my case vendor is Bluethink, Module is Overridetemplate and my file path is app/code/Bluethink/Overridetemplate/view/frontend/layout/ catalog_category_view.xml)

Step 4: Now, you can create your custom template in your module and Implement accroding to your requirements. In my case, my template file path is : app/code/Bluethink/Overridetemplate/view/frontend/template/product/list.phtml

Step 5: finally, run the following commands –

Please share your doubts in comment Section and also mentioned if this tutorial is helpful to you?

bluethinkinc_blog

bluethinkinc_blog

2022-07-27

1

One Reply to “How To Override Phtml File In Magento2”

  • Avatar
    Mohit

    Really helpful

    2022-07-27 at 11:46 pm Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

Implementation of WireMock for better testing

Spread the love WireMock is a flexible and powerful tool

Magento 2: How to selected default option in configurable options?

Spread the love Configurable products in Magento 2 allow

How To Implement Simple JWT Authentication in Django Rest Framework

Spread the love We’ll learn how to use simple JWT

Optimizing Search: Integrating Elasticsearch in Django Applications

Spread the love What is Elasticsearch? Elasticsearch is a

How to create Customer Segments

Spread the love In Magento 2, “customer

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

bluethinkinc Blogs