How to customize a functionality using Magento 2 Plugins(Interceptor)

How to customize a functionality using Magento 2 Plugins(Interceptor)

Magento 2 plugins come with 2 different versions, 1X and 2X.

As the number increased the features had also got a mark in 2X with a lot more improvements

In Version 1X, customization of different classes was challenging, whereas 2x managed to solve it.

There the overwriting of a core class in your modules was not us to the mark.

however the issue happens when there are at least two modules overwriting a similar core class,

2x Helps Magento Web Development Services with a wide range of Features.

How to customize a functionality using Magento 2 Plugins(Interceptor)

How Magento 2 features fix this core class issue?

Magento 2 introduced a new concept called Plugins. A plugin or interceptor is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. Plugins use the design pattern called Interception.

Let's see how Magento 2 plugins (interceptor) works

Restrictions in using Magento 2 Plugins are:

  • Final methods
  • Final classes
  • Non-public methods
  • Static methods
  • __construct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • Objects that are not instantiated by the ObjectManager (e.g. by using new keyword directly to create class object ).

Declaring a new plugin in Magento 2

You can create a new plugin for a class object using di.xml of your corresponding module at app/code/{namespace}/{module}/etc/di.xml.

magent2 plugin

Required options for a plugin

  • type name: Name of a class or interface which the plugin observes.
  • plugin name: An arbitrary plugin name that identifies a plugin. Also used to merge the configurations for the plugin.
  • plugin type: The name of a plugin’s class or its virtual type. Use the following naming convention when you specify this element: \Vendor\Module\Plugin\Plugin.

Optional options for a plugin

  • sortOrder: The order in which plugins that call the same method are run.
  • plugin disabled: To disable a plugin, set this element to true. The default value is false. Use this property to disable a core or third-party plugins in your di.xml file.

Types of plugins

In Magento 2, we have three types of plugins.

  • Before listener Plugin
  • After listener Plugin
  • Around listener Plugin

Before listener Plugin

You can use before plugin for changing the arguments of an original method or to add some behavior before an original method is called.

The name of this method will be the same as the original method with “before” as prefix and the first letter of an original method to capital.

Below is an example for before method to add additional behavior before the original method.

Step 1: Create di.xml at Vendor/Module/etc/di.xml

create plugin

Step 2: Create CustomFilter.php as plugin class at Vendor\Module\Plugin\CustomFilter.php

create customfilter

After listener Plugin

You can use after plugin for changing the value returned by the original method or to add some behavior after an original method is called.

The name of this method will be the same as the original method with “after” as prefix and the first letter of an original method to capital.

Below is an example for after method to add additional behavior after the original method.


Step 1: Create di.xml at Vendor/Module/etc/di.xml

after method

Step 2: Create SortFilter.php as plugin class at Vendor\Module\Plugin\SortFilter.php

create sortfilter

Around listener Plugin

You can use around plugin for changing both the arguments and value returned by the original method or to add some behavior before and after an original method is called.

The name of this method will be the same as the original method with “around” as prefix and the first letter of the original method to capital.

Below is an example for around method to add additional behavior before and after the original method.

Step 1: Create di.xml at Vendor/Module/etc/di.xml

around method

Step 2: Create ProductCart.php as plugin class at Vendor\Module\Plugin\ProductCart.php

create-plugin-class

                  Note: We can use Magento 2 plugin constructor (dependency injection for the plugin) if our class requires a dependency on other class methods or variables

Plugins Execution Order

We can prioritize plugins using sortOrder property when several plugins are observing the same method.

If we have two plugins name Plugin A, Plugin B and Plugin C with corresponding sort order 10, 20 and 30 observing the same method with the following properties:.

Plugin A Plugin B Plugin C
sortOrder 10 20 30
before beforeLoad() beforeLoad() beforeLoad()
around aroundLoad() aroundLoad()
after afterLoad() afterLoad() afterLoad()










The execution flow will be:

  • PluginA::beforeLoad()
  • PluginB::beforeLoad()
  • PluginB::aroundLoad()(until callable is called) 
    • PluginC::beforeLoad()
    • PluginC::aroundLoad() (until callable is called)
    • Action::load()
    • PluginC::aroundLoad() (after callable is called)
    • PluginC::afterLoad()
  • PluginB::aroundLoad() (after callable is called)
  • PluginB::afterLoad()
  • PluginA::afterLoad()

Conclusion

By using Magento 2 plugins we can hooks our logic into the available classes without overwriting them.

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn

leave a comment