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

Customize lead qualification for a seamless experience

Spread the love When the 2024 Release Wave 2 was launched,

Create a Custom Console Command in Spryker

Spread the love In Spryker, developers can create custom

SALESFORCE

Spread the love Salesforce is a cloud-based software

CRUD Operation in Spryker Backoffice with a Custom Module

Spread the love Spryker is a highly flexible and modular

How to create custom module on Spryker for Backoffice?

Spread the love Creating a custom module in Spryker

How to Install Spryker Project with Docker on Local Machine

Spread the love Spryker is a flexible and scalable commerce

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

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

bluethinkinc Blogs