How to create plugin in magento 2

How to create plugin in magento 2

Dear Friends

Today we will cover Topic, How you can create plugin in Magento2.

So What is Plugin?

A plugin, 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. This allows you to substitute or extend the behavior of original, public methods for any class or interface

What are the Benefits of using Plugin?

  • You can modify arguments which is used in method
  • You can modify result before return from method
  • You can change behaviour before, after or around any method without overriding any class and its method

Now we will understand the Limitations?

  • Plugin can work only with public method , So you cannot use it with private , protected , final methods etc.
  • Plugin cannot use for any class which contains atleast 1 final method
  • Plugin dosent work for final class
  • Plugin dosen’t work for constructor and desctructor
  • Plugin donsen’t work for virtual types

How we can declare Plugin?

<config>
    <type name="{ObservedType}">
      <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false" />
    </type>
</config>

Here we will understand terminology in simple language for above code

type name : Here you need to enter the class name with path for which you want to create plugin

plugin name : Here you need to enter unique name to define the plugin, it can be any name which identify your plugin

type : Here you need to enter class name with path which you will create for your plugin.

SortOrder : If you have same plugin multiple times so you can define sort order which plugin should call first and then after.

disabled : If you want to disable any plugin so you can use this attribute and make it true.

Here, I am adding 1 example so you can understand in more clear way.

 <config>
    <type name="Magento\Framework\App\Action\Action">
      <plugin name="vendor_action_plugin" type="Vendor\Module\Plugin\ActionPlugin" sortOrder="1" disabled="false" />
    </type>
</config>

If you can see, I have added Magento\Framework\App\Action\Action in type name bcz I want to create plugin for a method which is inside this class.

Next I have added plugin name and then added plugin class which is reside in my custom module.

Types of Plugin

We have 3 types of Plugin as below,

  1. Before Plugin
  2. After Plugin
  3. Around Plugin

Before Plugin

This plugin calls before it’s original function call, So if you want to change any parameter or any behaviour before original function call you can use this plugin.

Example

public function beforeGet(ProductRepositoryInterface $subject,$sku)
{
   return 'WJ01-S-Red';
}

I am changing here sku before calling get function of product repository function.

After Plugin

This plugin calls after it’s original function call, So if you want to change anything in result before returning or change any behaviour so you can use this plugin.

Example

public function afterGet(ProductRepositoryInterface $subject,$result)
{
   $result->setName("Test");
   return $result;
}

Here, I am changing product name before returning the result.

Around Plugin

This plugin is like override of method, This plugin calls before and after of observed method. If you want to change business logic of existing method before and after of that method so u can use this plugin.

Example

public function aroundGet(ProductRepositoryInterface $subject,callable $proceed,$sku)
{
   $result = $proceed('WJ01-S-Red');
   $result->setName("Test");
   return $result;
}

You can watch our video, below for more understanding with complete example.

If you are developer and want to learn Magento2 and other technologies so join my you tube channel and keep yourself updated.

Thanks for visiting us, and Stay connected

Subscribe to our newsletter

Leave a Reply
Your email address will not be published. *