Get Free Audit

How to Use Plugins in Magento 2

Jun 12, 2018

6326 Andrey Litvin

How to Use Plugins in Magento 2

Magento 2 Plugin (Interceptor) is a class that allows you to change the behavior of other classes’ methods by calling custom code before, after, or instead of calling specific methods. This reduces the probability of conflicts between different code sections representing the same functionality.

Plugin Configuration


, where:

{ObservedType} — the name of the class, whose method is to be changed;

{PluginName} — the name of the plugin;

{PluginClassName} — plugin’s class name;

sortOrder — plugin’s calling order;

disabled — the ability to make Magento ignore the plugin while leaving its configuration in place.

migration to magento 2

Magento 2 Migration

Take your online store to the next level with BelVG Magento 2 Migration

Visit the page

Plugin Methods

Before Methods

Before methods allow modifying the target class method arguments before this method is called.


The name of the plugin method is concatenated ‘before‘ and the name of the method, whose arguments are to be changed. The first argument of this method is the class whose method is called. The remaining arguments must match the arguments of the called method, including default values.

Before methods must return an array of arguments.

After Methods

After methods allow modifying the result of the target method.

Igor Dragun
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor


The first argument is the target class instance (like in before methods), the second is the return value of the original method.

Around Methods


Around methods allow you to execute code both before and after the target method in one place.

$proceed argument is PHP closure, which in turn calls the target method.

Such methods allow you to completely replace the target method.

Limitations

Unlike Magento 1 class rewrites, plugins do not inherit the target class, which facilitates the peaceful coexistence of several plugins that change the same method. But for the same reason, plugins have certain limitations.

Plugins cannot be used with:

  1. Final methods and classes
  2. Protected/private methods
  3. Static methods
  4. __construct methods
  5. Virtual types
  6. Objects that are instantiated before Magento\Framework\Interception is bootstrapped
Igor Dragun

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor

Plugin Sorting

Plugin sortOrder parameter allows you to determine in which order the plugin methods will be called in case multiple plugins are observing the same method.

Plugin1 Plugin2 Plugin3
sort order 10 20 30
before beforeMethod() beforeMethod() beforeMethod()
around aroundMethod() aroundMethod()
after afterMethod() afterMethod() afterMethod()

In this case, the processing of plugins will occur in the following order:

  1. Plugin1::beforeMethod()
  2. Plugin2::beforeMethod()
  3. Plugin2::aroundMethod() (Magento calls the first half until callable)
    1. Plugin3::beforeMethod()
    2. Plugin3::aroundMethod() (Magento calls the first half until callable)
      • TargetClass::method()
    3. Plugin3::aroundMethod() (Magento calls the second half after callable)
    4. Plugin3::afterMethod()
  4. Plugin2::aroundMethod() (Magento calls the second half after callable)
  5. Plugin1::afterMethod()
  6. Plugin2::afterMethod()

That is it regarding plugins in Magento 2, hope it was helpful. If you have any questtions on the article, please leave them in the comment section below.

Magento store.belvg.com

Magento Extensions

Take your online store to the next level with BelVG Magento Extensions

Visit the page
Igor Dragun
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor

6 Comments

  1. Thank you, Shahbaz! You are absolutely right, we have updated the article.

  2. Plugin sorting has a mistake in the scenario you presented. Point 6 will be executed before Point 5. So it should be swapped.

  3. Good article about magento methods this will be helpful for magento plugin users.

  4. Hi, Manisha! Thanks for your questions.
    If there is no sort order set for the plugin then Magento will use zero as sort order.
    Plugin-sorting method implementation can be found here:
    \Magento\Framework\Interception\PluginList\PluginList::_sort

  5. Nice Article for a plugin but I have a doubt,
    If apply 2 After plugins for the same method in the same module, without sortOrder, which plugin will be applied first? Please clarify.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *