Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Override Classes in Magento 2 Using Plugin | Unlocked

Vinh Jacker | 12-18-2024

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!


x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services