Web design company India | Custom web development company India
open

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

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

    In Magento, we can see two different versions Magento 1x and 2x. Magento 2x has come up with a lot more improvements compared to Magento 1x.

    In Magento 1, in order to customize different classes and method, where you can do without much of a stretch overwrite the core class in your module and Magento will begin utilizing your class rather than core class, however the issue happens when there are at least two modules overwriting a similar core class, for this situation just a single module will work fine yet different modules customization in the center class won't work.

    How Magento2 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

    Limitations of Magento 2 plugins

    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

    • 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

    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.

  • Post Comments

    Submit Comment




    * Required Fields