Plugins (Interceptors) in Magento 2

Plugins, also known as Interceptors, are one of the most powerful extension mechanisms in Magento 2. They allow developers to modify the behavior of public methods of a class without changing the original source code, ensuring upgrade safety and clean architecture.

What Are Magento 2 Plugins?

A Magento 2 plugin intercepts a public method call and gives you the ability to execute custom logic before, after, or around that method. Plugins work only with classes that are managed by Magento’s Object Manager.

How Many Types of Magento 2 Plugins?

Magento 2 provides three types of plugins:

  • Before Plugin
  • After Plugin
  • Around Plugin

Before Plugin

Before plugins run before the original method is executed. They can be used to modify the input arguments of the observed method.

<?php
namespace Vendor\Module\Plugin;

use Magento\Catalog\Model\Product;

class BeforeProduct
{
    public function beforeSetName(Product $subject, $name)
    {
        return ['[Custom] ' . $name];
    }
}
  

After Plugin

After plugins run once the original method has completed. They receive the result of the method and can modify it before returning.

<?php
namespace Vendor\Module\Plugin;

use Magento\Catalog\Model\Product;

class AfterProduct
{
    public function afterGetPrice(Product $subject, $result)
    {
        return $result * 1.10;
    }
}
  

Around Plugin

Around plugins wrap the original method and provide full control over its execution. The $proceed callback decides whether the original method should run.

<?php
namespace Vendor\Module\Plugin;

use Magento\Checkout\Model\Cart;

class AroundCart
{
    public function aroundAddProduct(
        Cart $subject,
        callable $proceed,
        $productInfo,
        $requestInfo = null
    ) {
        $result = $proceed($productInfo, $requestInfo);
        return $result;
    }
}
  

Defining a Plugin 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\Catalog\Model\Product">
        <plugin name="vendor_module_product_plugin"
                type="Vendor\Module\Plugin\BeforeProduct"
                sortOrder="10" />
    </type>

</config>
  

Magento 2 Plugin Limitations

  • Final classes or final public methods
  • Non-public methods (private or protected)
  • __construct and __destruct methods
  • Static methods
  • Virtual types

Why Use Plugins (Interceptors)?

  • Avoid core class rewrites
  • Reduce conflicts between multiple modules
  • Allow multiple plugins on the same method using sort order
  • Modify method arguments and return values
  • Maintain upgrade-safe customizations

Conclusion

Plugins in Magento 2 provide a clean, flexible, and reliable way to extend existing functionality. By using before, after, and around plugins, developers can inject custom business logic without breaking core behavior or future upgrades.