How To Create Plugin in Magento 2

Hello Everyone,

In this blog, we will learn about how to Create Plugins in Magento 2.

What is Plugin?

As per official documentation definition of plugin is:

A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface.

In simple words a plugin or interceptor is a class that extends the behavior of the original class.

Plugin only used with “public methods”.

Use of Plugin:

  • Minimum conflict among extensions that change the behavior of the same class.
  • Plugin is called sequentially according to sort order to avoid conflict with other plugin classes.

Types of Plugin:

There are three types of plugin

  1. Before Plugin
  2. After Plugin
  3. Around Plugin

Let’s discuss All plugin types.

  1. Before Plugin

Before Plugin is used when we want to change the argument of the original method,  or want to add some behavior before the original method. This method is called with the prefix ‘before’ with the original method’s first letter in capital.

  1. After Plugin

After Plugin is used when we want to change the argument of the original method, or want to add some behavior before the original method is called. This method is called with the prefix ‘after’ with the original method’s first letter in capital.

  1. Around Plugin

Around Plugin is used to change both arguments and returned values of an original method. It allows overriding a method. This method is called with the prefix ‘around’ with the original method’s first letter in capital.

Limitation of Plugin:

Plugin can not be used with following

Non-Public Methods

Static Methods

Final Methods

Virtual Types

__Construct

Any class that has at least one final public method

Objects that are instantiated before Magento\Framework\Interception is bootstrapped 

Without wasting your time, let us guide you straight away. Follow the easy step given below to Create Plugin in Magento 2.

Example of All Types of Plugins.

STEPS FOR CREATE PLUGIN IN MAGENTO 2

  1. Before Plugin:

Step 1: Create di.xml file

app/code/Vendor/Extension/etc/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="productqty" type="Vendor\Extension\Plugin\Product Qty" sortOrder="1" />
    </type>
</config>

Step 2: Create Productqty.php file

app/code/Vendor/Extension/Plugin/Productqty.php

<?php
namespace Vendor\Extension\Plugin;
class Productqty
{
    public function beforeAddProduct(
    \Magento\Checkout\Model\Cart $subject,
    $productInfo,
    $requestInfo = null
    ) {
        $requestInfo['qty'] = 2; // increasing quantity to 2
        return array($productInfo, $requestInfo);
    }
}

Using the before plugin when we click on add to cart button product quantity will increase with 3 quantities.

Step 3: Output:

  1. After Plugin:

Step 1: Create di.xml file

app/code/Vendor/Extension/etc/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\Catalog\Model\Product">
        <plugin name="nameadd" type="Vendor\Module\Plugin\Nameadd" sortOrder="1" />
    </type>
</config>

Step 2: Create Nameadd.php file

app/code/Vendor/Extension/Plugin/Nameadd.php

<?php
namespace Vendor\Extension\Plugin;

class Nameadd
{
    public function afterGetName(\Magento\Catalog\Model\Product $subject, $result) {
        return "Magecurious ".$result; // Magecurious will add before Product Name
    }
}

Using the after plugin we can add name before product name.

Step 3: Output:

  1. Around Plugin

Step 1: Create di.xml file

app/code/Vendor/Extension/etc/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\Catalog\Model\Product">
        <plugin name="addProduct" type="Vendor\Extension\Plugin\Product" sortOrder="1" />
    </type>
</config>

Step 2: Create Product.php file

app/code/Vendor/Extension/Plugin/Product.php

<?php
namespace Vendor\Module\Plugin;

class Product
{
    public function aroundAddProduct(
    \Magento\Checkout\Model\Cart $subject,
    \Closure $proceed,
    $productInfo,
    $requestInfo = null
    ) {
        $requestInfo['qty'] = 3; // Increased quantity to 5
        $result = $proceed($productInfo, $requestInfo);
        // Apply logic here
        return $result;
    }
}

Finally run the below commands

$ php bin/magento setup:di:compile
$ php bin/magento cache:clean
$ php bin/magento cache:flush

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Create Plugins in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

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