Shopping cart

Magento 2 Override Classes Using Plugin

There are various ways to override classes and methods. Among them, using a class preference can be considered as the most workable and straightforward way in most situations. However, rewriting by this method can sometimes cause conflicts. In order to help you deal with that, a new concept of plugins is introduced. It would help extend the methods without changing the class itself as the class preference method. In this tutorial, we will show you how to override a class using plugin.

Each module has a global and are-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>

How to override classes using plugin

  • Before method (#before)
  • After method (#after)
  • Around method (#around)

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 to use for overriding methods.





Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
override
classes
using
plugin

Comments for Magento 2 Override Classes Using Plugin

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend: