A plugin or interceptor can be defined as a way of inserting code dynamically without making any changes in the original class behaviour. Facilitates extending the core functionalities without making any changes in the core files.
Magento 2 comes with interceptors and plugins. In this method, I will tell you how to use, its restrictions, types and the method to create plugin in Magento 2.
Reasons to Use Interceptors or Plugins in Magento 2
The change in the behaviour of same class or method is because of minimum conflicts among extensions.
With the help of sort order attribute plugin, make sure that collisions are avoided between the plugins.
Ensure that the plugin is called sequentially as per the sort order and so the conflict with other plugins is avoided.
Facilitates to customize the similar method in different modules.
Can easily change the return value of any method call that can be used on an object manager controlled object.
Magento 2 Plugins can’t be used with
Non-public methods
Static methods
Final methods
Final classes
Types of Interceptors in Magento 2
Before listener
After listener
Around listener
It is crucial to follow certain steps to create Plugin in Magento 2. To get detailed information, refer to the tutorial at: https://meetanshi.com/blog/create-plugin-interceptor-in-magento-2/
No comments:
Write comments