How to Override a phtml File Using a Custom Module in Magento 2

How to Override a phtml File Using a Custom Module in Magento 2

It is well known that customizing the core Magento 2 functionality is a bad coding practice. Once you upgrade the version, your efforts are in vain. Hence, it is advisable to override a function, class or file, as per the requirement.

Here, I’ll show you how to override a phtml file using a custom module in Magento 2.

There are two methods to do so as mentioned below:

Methods to Override a phtml file using a custom module in Magento 2:

  1. Create file registration.php at app/code/Vendor/Extension/ folder

  2. Create module.xml file at app/code/Vendor/Extension/etc/ folder
  1. Create file customer_account_create.xml at app/code/Vendor/Extension/view/frontend/layout/ folder
     

now you need to create register.phtml at location app/code/Vendor/Extension/view/frontend/templates/form/

That’s it.

The above example shows how to override a phtml file for product stock status. The default functionality to show out of stock is overridden to display an image.

Follow any of the above method suitable to you.

Please share your doubts on the topic in the Comments section below. I’d be happy to help.

Thank you.

 

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

4.9
(based on 19 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

2 comments On How to Override a phtml File Using a Custom Module in Magento 2

Leave a reply:

Your email address will not be published.