Magento, Magento 2, PHP

Maximizing Magento 2 Application with the Flexibility of Interceptors

Maximizing Your Magento 2 Application with the Flexibility of Interceptors

Magento 2 interceptors, also known as plugins, are a powerful tool that allows developers to modify the behavior of a class without changing its code. This is achieved by intercepting method calls and executing code before, after, or around the original method.

Interceptors are implemented using the plugin design pattern, which is a structural design pattern that allows developers to add new functionality to an existing object without altering its structure. In Magento 2, interceptors are implemented using the Plugin class, which provides the necessary infrastructure to create and register plugins.

There are several types of interceptors available in Magento 2, including before, after, and around interceptors. Each type of interceptor allows developers to execute code at a specific point in the method call flow.

Before interceptors are executed before the original method is called. They can be used to modify the input parameters or perform some other action before the original method is called.

After interceptors are executed after the original method is called. They can be used to modify the output of the original method or perform some other action after the original method is called.

Around interceptors are executed both before and after the original method is called. They can be used to modify the input and output of the original method or perform some other action before and after the original method is called.

<?php

namespace My\Module\Plugin;

class ProductNamePlugin
{

    /**
     * Before plugin
     *
     * @param \Magento\Catalog\Model\Product $subject
     * @param string $name
     * @return array
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function beforeSetName(
        \Magento\Catalog\Model\Product $subject,
        $name
    ) {
        // Modify the product name before it is set
        $name = strtoupper($name);
        return [$name];
    }

    /**
     * After plugin
     *
     * @param \Magento\Catalog\Model\Product $subject
     * @param \Magento\Framework\Model\AbstractModel $result
     * @return \Magento\Framework\Model\AbstractModel
     */
    public function afterGetName(
        \Magento\Catalog\Model\Product $subject,
        \Magento\Framework\Model\AbstractModel $result
    ) {
        // Modify the product name after it is retrieved
        $result = strtolower($result);
        return $result;
    }

    /**
     * Around plugin
     *
     * @param \Magento\Catalog\Model\Product $subject
     * @param callable $proceed
     * @param string $name
     * @return \Magento\Framework\Model\AbstractModel
     */
    public function aroundSetName(
        \Magento\Catalog\Model\Product $subject,
        callable $proceed,
        $name
    ) {
        // Modify the product name before and after it is set
        $name = strtoupper($name);
        $result = $proceed($name);
        $result = strtolower($result);
        return $result;
    }
}

To use this plugin, you will also need to create a di.xml file in your module’s etc directory and register the 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="my_module_product_name_plugin" type="My\Module\Plugin\ProductNamePlugin" sortOrder="10"/>
    </type>
</config>

In Magento 2, the callable parameter of the around the plugin method is a closure that represents the original method being intercepted. The callable parameter is passed to the around plugin method as an argument, and it can be used to execute the original method at any point during the plugin’s execution.

Interceptors are a useful tool for developers working with Magento 2. They allow developers to modify the behavior of a class without changing its code, making it easier to add new functionality and customize the behavior of an application.

If you are working with Magento 2 and want to customize the behavior of your application, be sure to consider using interceptors to take advantage of the full power of the plugin design pattern. So, this is how you can use Magento 2 interceptors in your code to modify the behavior of a class.

Please let me know what you think about this blog in the comment section below, till next time Happy Coding 😀

Tagged , , ,

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.