Magento 2

How to Override core code using Custom Plugin in Magento 2

When it comes to fulfill digital needs and give the personalized touch, it always ends up with customization. To customize default Magento store functionality, you need to make changes in default Magento core function files. But, we have never recommended modifying default Magento core files, but an alternative way is to override core files which is a safe and easy way to achieve customization. Compared to Magento 1, overriding and manipulating with classes is becoming much easier for a developer in Magento 2. Also, it allows us more ways of extending the native functionalities.

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

Among this, we suggest you use the plugin method to rewrite all files in a convenient way. Picking up plugin from a list is a clever choice because it helps us to reduce conflicts. The plugin offers you an extensive functionality to execute your custom code before, after or sequentially with the specified function or event.

Basically, there are three different types of plugins :
1. Before Method
2. After Method and
3. Around

As the name suggests these plugins methods allows us to execute our custom logical code before, after or around our observing methods. Let’s take a deep dive and understand how to implement different types plugins.
Just like Preferences, plugins are also declared in etc\di.xml file available at below path.
app\code\Vendor\Extension\etc\frontend\di.xml



    
        
    

Once, you have declared your plugin, you can proceed with implementing these plugins methods. For better understanding, here we have used these three different methods with the cart before, after and around actions.
You just need to create ‘Plugincart.php’ file at below location and add your method code and logic.
app\code\Vendor\Extension\Model\Plugincart.php

1) Before
In before method, the first argument will be always class object and rest will be Original Function Argument.


When you are using after method, your first argument will be always class object and Second will be Result, so you can easily modify the result.

The last method is around and when you are using around method, your first argument will be the class object and rest will be callable that allows you to call original function with arguments.

Comment down below if you are looking for any help regarding this code.
Happy Coding!
Click to rate this post!
[Total: 9 Average: 3.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.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago