Create a new plugin in Magento 2
The plugins (also known as Interceptor) is a class that modify the behavior of public class by intercepting the function call and running code before, after or with (around) function call. By creating the plugin we create substitute or extend the functionality of existing functionality (the same that we were doing in magento 1 rewrite). In this article we will see how to create a plugin (or how to override the existing function of any class).
Its a two step process:
i) Declaring a plugin: Create a file di.xml under etc folder of your moduel . See di.xml in app\code\Cutehits\First\etc
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Checkout\Model\Cart"> <plugin name="CutehitsInterceptor" type="Cutehits\First\Plugin\PluginBefore" /> </type> </config>
In the above code the only important code is as below:
<type name=”Magento\Checkout\Model\Cart”>
<plugin name=”CutehitsInterceptor” type=”Cutehits\First\Plugin\PluginBefore” />
</type>
remaining code is boilerplate code. Lets understand the meaning of each tag.
type name
: It is a class or interface which the plugin observes.plugin name
: It is an arbitrary plugin name that identifies a plugin. Also used to merge the configurations for the plugin.plugin type
: This is the name of a plugin’s class or its virtual type. Use the following naming convention when you specify this element:\Vendor\Module\Plugin\<ModelName>Plugin
.
The following elements are optional:
plugin sortOrder
: The order in which plugins that call the same method are run.plugin disabled
: To disable a plugin, set this element totrue
. The default value isfalse
.- Use this property to disable core or third-party plugins in your
di.xml
file.
- Use this property to disable core or third-party plugins in your
By apply optional parameter the code will be something like
<type name=”Magento\Checkout\Model\Cart”>
<plugin name=”CutehitsInterceptor” type=”Cutehits\First\Plugin\PluginBefore” sortOrder=”1″ disabled=false/>
</type>
ii) Defining a plugin:
To define a plugin create a folder named Plugin your module. Lets take a look of app\code\Cutehits\First\Plugin\PluginBefore.php
namespace Cutehits\First\Plugin; use Magento\Checkout\Model\Cart; //use Magento\Framework\Logger\Monolog; class PluginBefore { /** * Wraps the input to the base method in (before)(/before) tags. * * The before plugin returns an array, which will be the array of arguments given to the base method. * * @param ChildBefore $subject * @param string $interceptedInput * @return string * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeAddProduct( \Magento\Checkout\Model\Cart $subject, $productInfo, $requestInfo = null ) { //die("Before Add to Product Calling"); $requestInfo['qty'] = 10; // increasing quantity to 10 return array($productInfo, $requestInfo); } public function afterAddProduct( \Magento\Checkout\Model\Cart $subject, $productInfo, $requestInfo = null ) { die("After Add to Product Calling"); } public function aroundAddProduct() { die("Around Add to Product Calling"); } }
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments