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

Custom API to Fetch Customer Data by Customer ID in Spryker

Spread the love Introduction In this blog post, we will

Creating a Custom Module for a Custom Frontend Page in Spryker

Spread the love To create a custom module for a custom

How to create custom module api get category list without access token in Spryker

Spread the love To create a custom API for fetching a

Creating a Custom Glue API in Spryker

Spread the love Spryker’s Glue API is a powerful tool

Get AI Generated 360-Degree View of Account Summary in Dynamics 365 Sales

Spread the love Navigating the demanding landscape of sales

Enhancing Productivity with Dynamics 365: The Power of Session Restore

Spread the love Enhancing Productivity with Dynamics 365:

Salesforce Admin

Spread the love In our last blog, we discussed common

Unlocking AI with Spring Boot: Real-World Use Cases and Solutions

Spread the love Artificial Intelligence (AI) has moved from

Enhancing Performance with Reactive Java: A Beginner-Friendly Guide

Spread the love In today’s fast-paced software

Integrating Azure Function with Dynamics 365

Spread the love Signup on azure portal and make sure to

bluethinkinc Blogs