In this blog we will discuss about new feature of Magento 2 Plugins. There is a design pattern called ‘Interception’ used in Plugins.
Interception is inserting code dynamically without changing the original class behaviour. In this process code is dynamically inserted between the calling code and the target object.
You can check the overview in the video below —
Types of Magento 2 Plugins
We can create and use three types of Magento 2 Plugins.
- Before listener Plugin
- After listener Plugin
- Around listener Plugin
1. Before Listener
Before listeners are used whenever we want to change the arguments of an original method or want to add some behavior before an original method is called.
We will use before listeners to change behavior of addProduct method of Magento\Checkout\Model\Cart.
This method is called whenever we add product to cart.
Original Method is
public function addProduct($productInfo, $requestInfo = null)
To use Plugins first of all we have to define it in di.xml.
<?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\Model\Cart"> <plugin name="WebkulCart" type="Webkul\Test\Model\Cart" sortOrder="1" /> </type> </config>
Now create file Cart.php in folder ‘Webkul\Test\Model’.
Before listener is called by adding prefix ‘before’ to the method name and setting first letter of original method to capital.
Now method addProduct will become beforeAddProduct.
<?php
namespace Webkul\Test\Model;
class Cart
{
public function beforeAddProduct(
\Magento\Checkout\Model\Cart $subject,
$productInfo,
$requestInfo = null
) {
$requestInfo['qty'] = 10; // increasing quantity to 10
return array($productInfo, $requestInfo);
}
}
If there are any arguments, the method should return an array of those arguments. If the method does not change the argument for the observed method, it should return a null value.
Here we are changing parameters. We set quantity to 10. Now it will always add 10 quantities of the product whenever we add product to cart.
So we will use before listener when we want to change parameter of an method.

2. After Listener
After listeners are used when we want to change values returned by an original method or want to add some behavior after an original method is called.
We will use after listeners to change behavior of getName method of Magento\Catalog\Model\Product.
This method returns the name of Product.
Original Method is
public function getName()
<?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\Catalog\Model\Product"> <plugin name="WebkulCart" type="Webkul\Test\Model\Product" sortOrder="1" /> </type> </config>
Now create file Product.php in folder ‘Webkul\Test\Model’.
Magento calls an after listener by adding the prefix after to the method name and capitalizing the first letter of the original method.
Now method getName will become afterGetName.
<?php
namespace Webkul\Test\Model;
class Product
{
public function afterGetName(\Magento\Catalog\Model\Product $subject, $result) {
return "Apple ".$result; // Adding Apple in product name
}
}


3. Around Listener
Around listeners are used when we want to change both the arguments and the returned values of an original method or add some behavior before and after an original method is called.
We will use around listeners to change behavior of addProduct method of Magento\Checkout\Model\Cart.
Magento calls an around listener by prefixing the method name with around and capitalizing the first letter of the original method.
Now method addProduct will become aroundAddProduct.
<?php
namespace Webkul\Test\Model;
class Cart
{
public function aroundAddProduct(
\Magento\Checkout\Model\Cart $subject,
\Closure $proceed,
$productInfo,
$requestInfo = null
) {
$requestInfo['qty'] = 10; // setting quantity to 10
$result = $proceed($productInfo, $requestInfo);
// change result here
return $result;
}
}
The around listener method forms the return value by passing the parameters that follow the $closure parameter in the method definition to the $closure function call in sequential order.
That’s all for Magento 2 Plugin System.
If you have any query please comment below.
2 comments
First, “after” methods, as specified in the Magento documentation, required to have a return value.
“These methods can be used to modify the results of an observed method and are required to have a return value.”
Second, the method name in wrong. The following line :
“Now method addProduct will become afterGetName.
should be :
Now method getName will become afterGetName.