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.
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’.
After listener is called by adding prefix ‘after’ to the method name and setting first letter of original method to capital.
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.
Around listener is called by adding prefix ‘around’ to the method name and setting first letter of original method to capital.
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 return value is formed in such way that the parameters following the $closure parameter in the around listener method definition are passed to the $closure function call in a sequential order.
That’s all for Magento 2 Plugin System.
If you have any query please comment below.
For better understanding you can refer to this video Magento Meetup New Delhi – Magento 2 Plugins.
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.