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 to true. The default value is false.
    • Use this property to disable core or third-party plugins in your di.xml file.

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");
}

}

 

Related posts:

The following two tabs change content below.

Chandra Shekhar

Consultant at Infogain
Chandra Shekhar is a LAMP Developer,Thinker,Blogger and currently working as Magento(Ecommerce) specialist.. I am always open to learn new things in LAMP as well as other newer technology. In the journey of my last 9 years of web and product development, I have decided to share my knowledge and experience to Open Source Community in 2011. In this regard I have started CuteHits.com. Since then I am actively sharing my knowledge on various LAMP technology through CuteHits.com’s technical ... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)