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
1 2 3 4 5 6 |
/app/code/Vendor/Module/etc/di.xml <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <type name="{ObservedType}"> <plugin name="{PluginName}" type="{PluginClassName}" sortOrder="1" disabled="false"/> </type> </config> |
, 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.
Magento 2 Migration
Take your online store to the next level with BelVG Magento 2 Migration
Visit the pagePlugin Methods
Before Methods
Before methods allow modifying the target class method arguments before this method is called.
1 2 3 4 5 6 7 8 |
Namespace Vendor\Module\Plugin; class Plugin { public function beforeMethodName(\Vendor\Module\Model\TargetClass $subject, $arg1, $arg2, $arg3 = null) { return [$arg1, $arg2, $arg3]; } } |
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.
1 2 3 4 5 6 7 8 |
Namespace Vendor\Module\Plugin; class Plugin { public function afterMethodName(\Vendor\Module\Model\TargetClass $subject, $result) { return $result; } } |
The first argument is the target class instance (like in before methods), the second is the return value of the original method.
Around Methods
1 2 3 4 5 6 7 8 9 |
Namespace Vendor\Module\Plugin; class Plugin { public function aroundMethodName(\Vendor\Module\Model\TargetClass $subject, callable $proceed, $arg1, $arg2, $arg3) { $result = $proceed(); return $result; } } |
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:
- Final methods and classes
- Protected/private methods
- Static methods
- __construct methods
- Virtual types
- Objects that are instantiated before Magento\Framework\Interception is bootstrapped
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.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:
- Plugin1::beforeMethod()
- Plugin2::beforeMethod()
- Plugin2::aroundMethod() (Magento calls the first half until callable)
- Plugin3::beforeMethod()
- Plugin3::aroundMethod() (Magento calls the first half until callable)
- TargetClass::method()
- Plugin3::aroundMethod() (Magento calls the second half after callable)
- Plugin3::afterMethod()
- Plugin2::aroundMethod() (Magento calls the second half after callable)
- Plugin1::afterMethod()
- 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 Extensions
Take your online store to the next level with BelVG Magento Extensions
Visit the page
Nice, thank you for sharing it!
Thank you, Shahbaz! You are absolutely right, we have updated the article.
Plugin sorting has a mistake in the scenario you presented. Point 6 will be executed before Point 5. So it should be swapped.
Good article about magento methods this will be helpful for magento plugin users.
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
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.