How To

How to Override Framework Class in Magento 2

When you want to serve personalized experience to your shoppers by making changes to default layout, it always ends up at Magento Customization. Over the time Magento has topped Ecommerce CMS list with its functionality, features and power of extending the native functionalities. Compared to Magento 1, In Magento 2 overriding and manipulating with classes is becoming easier a developer to play quickly. At MageComp, we have never recommended modifying default core functionalities, but we can extend core functionalities using concepts of overriding which is a safe and easy way to achieve customization or development.

By default, there are three different ways to override core functionalities.
1. Using preference
2. Using event\observer
3. Using a plugin

While using overriding, sometimes you need to override the class, which reside in the framework. At that time, with the help of this tiny piece of code inside your custom extension, it will be easy for you.

For example, we need to override the Title.php of “vendor\magento\framword\View\Page“.
Then simply add this line to your di.xml file of your module/extension.

<pre class="lang:default decode:true">
<preference for="Magento\Framework\View\Page\Title" type="[VENDOR]\[EXTENSION]\View\Page\Title" />
</pre>

Now, create the class [VENDOR]\[EXTENSION]\View\Page\Title that extends Magento\Framework\View\Page\Title and make your changes in that.

That’s it! You have successfully override framework class in Magento 2. You are free to play and manipulate this according to your need for overriding one or more class in Magento 2 Extension.

That’s it for today, Let us know if you are facing an issue while implementing using this code by commenting below.

Happy Overriding!

Click to rate this post!
[Total: 7 Average: 4.4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago