How to create and use Plugins(Interceptors) in Magento 2

In this article we will learn about ‘Plugin’ that is new feature in Magento2. 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.

Interception is insert code dynamically without change the native class behaviour. In this procedure code is dynamically inserted between the calling code and the target object.

Types of Plugins in Magento2
A Plugins uses with three mechanisms to intercept the public methods:
1) before plugin
2) after plugin
3) around plugin

A Plugin have some limitations, you cannot used plugin on following:

  • Final methods
  • Final classes
  • Non-public methods
  • Class methods (such as static methods)
  • __construct and __destruct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped

Let’s start to learn create and use of plugin in magento 2.

Declare Before Plugin

<?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="before_plugin_name_product_change" type="Mageclues\Override\Plugin\Name" sortOrder="10" disabled="false"/>
    </type>
</config>

Defining Before Plugin

<?php

namespace Mageclues\Override\Plugin\Name;

use Magento\Catalog\Model\Product as MainProduct;

class Name
{
     /**
     * @param MainProduct $subject
     * @param $result
     * @return mixed
     */
    public function beforeGetName(MainProduct $subject)
    {
        $brand = " - Brand_Title";
        $result .= $brand;

        return $result;
    }
}

Declare After Plugin

<?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="after_plugin_name_product_change" type="Mageclues\Override\Plugin\Name" sortOrder="10" disabled="false"/>
    </type>
</config>

Defining After Plugin

<?php

namespace Mageclues\Override\Plugin\Name;

use Magento\Catalog\Model\Product as MainProduct;

class Name
{
	/**
     * @param MainProduct $subject
     * @param $result
     * @return mixed
     */
    public function afterGetName(MainProduct $subject, $result)
    {
        if ($result) {
            $brand = " - Brand_Title";
            $result .= $brand;
        }

        return $result;
    }
}

Declare Around Plugin

<?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="around_plugin_name_product_change" type="Mageclues\Override\Plugin\Name" sortOrder="10" disabled="false"/>
    </type>
</config>

Defining Around Plugin

<?php

namespace Mageclues\Override\Plugin\Name;

use Magento\Catalog\Model\Product as MainProduct;

class Name
{
	/**
     * @param MainProduct $subject
     * @param $result
     * @return mixed
     */
    public function aroundGetName(MainProduct $subject, callable $proceed)
    {
        $brand = " - Brand_Title";
        $result .= $brand;
        return $result;
    }
}

We hope this article is helpful for you. if any query please leave comment, we will reply to you.

Thank You):

Related Posts

Leave a Reply

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