Recent Blogs

  • Why A Travel Company Needs An Attractive Website?

    A website considered a great source of a marketing platform for a travel company. It is a place where you can share your availability and regularity all the time without unusual hindrances. A website should be responsive, user-friendly, and easy to operate. A travel company promotes itself through the best website to put trustworthy content. It may promote visitors and it would be a great platform to place tour packages.

    Why travel agencies needs a attractive website


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

Here we have explained about customize a functionality using Interceptor in Magento 2 Plugins:
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
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.

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

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

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 APlugin BPlugin C


The execution flow will be:
PluginB::aroundLoad()(until callable is called)
PluginC::aroundLoad() (until callable is called)
PluginC::aroundLoad() (after callable is called)
PluginB::aroundLoad() (after callable is called)


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

Leave a Reply

Your email address will not be published. Required fields are marked *