How to Override Classes in Magento 2 Using Plugin | Unlocked

There are various ways to override classes and methods. Among them, using a class preference can be considered the most workable and straightforward way in most situations. However, rewriting by this method can sometimes cause c Chèn conflicts. In order to help you deal with that, another method is using Plugin. It would help extend the methods without changing the class itself as the class preference method.

If there are many plugins hooked onto a target class, Magento will just execute them sequentially based on the sortOrder parameter in your file di.xml. This is because when using this method, your core class is not replaced by your plugin class.

In this tutorial, we will show you how to override a class using plugin.

Each module has a global and specific di.xml files which can be applied based on scope. Below are the paths for module di.xml files:

Mageplaza/HelloWorld/etc/di.xml
Mageplaza/HelloWorld/etc/<area>/di.xml

Plugins can also be configured in the di.xml files, and they are called before, after and around the methods which are being overridden. Below is an example of the di.xml file when we are going to :

<config>
    <type name="Magento\Catalog\Api\Data\ProductInterface">
        <plugin name="mageplaza_helloworld_catalog_product" type="Mageplaza\HelloWorld\Plugin\Model\Product" />
    </type>
</config>

Now let’s get to the detailed method for overriding classes in Magento 2 using Plugin.

How to override classes using plugin

How to override classes using plugin

Before method

Before running plugin priorly to an observed method, if the method is not being modified, the same argument number has to be returned in an array that the method accepts or null. The method which is being extended need to have the identical name with prefix before

<?php
namespace Mageplaza\HelloWorld\Plugin\Model;
 
class Product
{
    public function beforeSetPrice(\Magento\Catalog\Model\Product $subject, $price)
    {
        $price += 10;
        return [$price];
    }
}

After method

Once the original method has been called, after methods would be executed. Besides a class object, the method accepts another argument and that also the result that must be returned. The method which is being extended has to have similar name with prefix after.

<?php
namespace Mageplaza\HelloWorld\Plugin\Model;
 
class Product
{
    public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
    {
        $result .= ' (Mageplaza)';
        return $result;
    }
}

Around method

With around method, the original method will be wrapped. Also, this method allows code execution both before and after the original method. It accepts another argument receives is callable which enables other plugins to call in the chain. The method that is being extended has to have similar name with prefix around.

<?php
namespace Mageplaza\HelloWorld\Plugin\Model;
 
class Product
{
    public function aroundSave(\Magento\Catalog\Model\Product $subject, \callable $proceed)
    {
        // before save
        $result = $proceed();
        // after save
 
        return $result;
    }
}

Conclusion

In conclusion, although using plugins does come with several limitations as it cannot be used for all types of methods, it’s still the ideal solution that you can consider using for overriding methods. I hope that you find a helpful tutorial. Thanks for reading!


Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


Image Description
Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.
x