How to override a plugin in Magento 2

Magento 2 plugins are fundamental parts of the system. Magento plugins are the name for interceptors and plugin code is auto-generated and stored in the /generation folder.

One way to change the functionality of a plugin is to disable it entirely and redefine a plugin for the same type.

To disable a plugin, one can easily do it using the disable attribute.

Depending on the requirements, it’s also possible to set the sortOrder attribute for our plugin to a smaller number and return earlier from our “before” interceptor preventing the next plugin in the queue from running. However, for example, let’s stay with the original idea of overriding the plugin by disabling the original.

Add the code in app/code/{Vendor}/{Module}/etc/{area}/di.xml as an example below:

<type name="Magento\Checkout\Model\ShippingInformationManagement">
    <plugin name="old_custom_checkout_pickup_store" disabled="true" />
    <plugin name="new_custom_checkout_pickup_store" type="Techurbane\StoreSwitcher\Plugin\Checkout\Model\ShippingInformationManagement" sortOrder="1"/>

As you can see, plugins can also be overridden, but it’s better to think about this as we are reordering or disabling them rather than overriding them. It’s also impossible to define a plugin around another plugin.

Happy Coding!

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments