A plugin, or interceptor, is a class that modifies the behavior of original public class functions or methods by another 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. Plugins only use for “public methods”.
Simplifying the words,
“A plugin or interceptor is a way to insert code dynamically without changing the original class behavior. It allows extending the core functionality without any modification to the core files.”
Hopefully, for beginners, the term “plugin” is not confused with the “module” as they are the synonyms for platforms like WordPress. But not in Magento!
Magento 1 permitted to customize various classes and methods by rewriting a class. Powerful, but in this way, no modules could rewrite the same class, and hence, no adaptability. To overcome the rewrite conflicts and instability, Magento 2 comes with interceptors or plugins! And, the post is everything about it from the reasons to use, its restrictions, types and the method to create a plugin in Magento 2.
Types Of Plugins In Magento2
In Magento2 we can create and use three types of Plugin.
1. Before Plugin
2. After Plugin
3. Around Plugin
1) Before Plugin:
Before Plugin is used for changing the arguments of an original method or add some behavior before an original method called. To use before plugin, we need to use before prefix and its added to the name of an original method. The before plugin methods do not need to have a return value.
Prototype: beforeMethodname()
2) After Plugin:
After Plugin is used for changing the arguments of an original method or add some behavior before an original method called. To use after plugin, we need to use after prefix and its added to the name of an original method. The after plugin methods do not need to have a return value.
Prototype: afterMethodname()
3) Around Plugin:
After Plugin is used for changing both the arguments and returned values of an original method or add some behavior before and after an original method is called. We need to use it after prefix and its added to the name of an original method. The around plugin must have a return value.
Prototype: aroundMethodname()
Steps to Create Plugin in Magento 2:
1. Create registration.php file in app\code\[Vendor]\[Namespace]\
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, '[Vendor]_[Namespace]', __DIR__ ); |
2. Create module.xml file in app\code\[Vendor]\[Namespace]\etc
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="[Vendor]_[Namespace]" setup_version="1.0.0"/> </config> |
3. Create di.xml file in app\code\[Vendor]\[Namespace]\etc
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Checkout\Controller\Index\Index"> <plugin name="restrictCartQuantity" type="[Vendor]\[Namespace]\Plugin\Checkout\Controller\RestrictProductQuantity"/> </type> </config> |
4. Create RestrictProductQuantity.php in [Vendor]\[Namespace]\Plugin\Checkout\Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace [Vendor]\[Namespace]\Plugin\Checkout\Controller; class RestrictProductQuantity { public function aroundExecute( Index $subject, \Closure $proceed ) { //You required logic } } |
Do not delay to use the Comments section below if you have any doubts with Magento 2 plugins.