Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Magento2 – Create and Use Plugins

    In this blog we will discuss about new feature of Magento2 that is ‘Plugins’.
    You can refer to Youtube video also. Magento Meetup New Delhi- Magento2 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 Plugins in Magento2

    In Magento2 we can create and use three types of Plugin.

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

    The before listener methods do not need to have a return 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.
    Plugins
    Plugins

    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 addProduct 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
    		}
    	}

    The after listener methods do not need to have a return value.
    Plugins
    Plugins

    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 ‘arround’ 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 around listener methods must have a return value.
    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 Plugin System in Magento2.
    If you have any query please comment below.

    For better understanding you can refer to this video Magento Meetup New Delhi- Magento2 Plugins.

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Rahul S
    Nice article
  • Oğuz Çelikdemir
    There are some typo problems.

    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.

  • Back to Top