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

}

 

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...