Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    magento-hyperlocal-marketplace-guide

    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.


    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.


    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.

    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()


    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.


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

    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.


    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.

    . . .

    Comments (2)

    Add Your Comment

  • 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
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close