Magento 2 plugins are a type of extension that allow developers to modify the core functionality of the Magento 2 platform without changing the core code. This means that developers can add new features or modify existing ones without altering the core code, which makes it easier to upgrade the platform and maintain stability.

There are two types of Magento 2 plugins: before and after plugins. Before plugins are called before the original method is executed, and after plugins are called after the original method is executed. Let’s take a look at some code examples to help illustrate the difference between these two types of plugins.

Before Plugins

Before plugins are used to modify the input parameters of a method before it is executed. For example, let’s say we want to modify the price of a product before it is added to the cart. Here’s how we might do that with a before plugin:

class ModifyPriceBeforePlugin
{
    public function beforeAddProduct(
        \Magento\Checkout\Model\Cart $subject,
        $product,
        $requestInfo = null
    ) {
        $product->setPrice($product->getPrice() * 1.1);
        return [$product, $requestInfo];
    }
}

In this example, we’re using a before plugin to modify the price of the product by 10% before it is added to the cart. The beforeAddProduct method is called before the original addProduct method is executed, so the modified price will be used in the original method.

After Plugins

After plugins are used to modify the output of a method after it has been executed. For example, let’s say we want to add a custom message to the checkout success page. Here’s how we might do that with an after plugin:

class AddMessageAfterPlugin
{
    public function afterExecute(
        \Magento\Checkout\Controller\Onepage\Success $subject,
        $result
    ) {
        $customMessage = 'Thank you for your purchase!';
        $result->getLayout()->getBlock('checkout.success')->setCustomMessage($customMessage);
        return $result;
    }
}

In this example, we’re using an after plugin to add a custom message to the checkout success page. The afterExecute method is called after the original execute method is executed, so the custom message will be displayed on the success page.

Around Plugins

A Magento 2 around plugin is a type of plugin in the Magento 2 framework that allows developers to modify the behavior of a method by wrapping it in a new function. This allows developers to add additional functionality to an existing method without changing the original code.

For example, suppose a Magento 2 store has a method called “sendEmail” that is responsible for sending emails to customers. A developer may want to add the ability to log all emails that are sent, but does not want to modify the original “sendEmail” method. In this case, the developer could create an around plugin that wraps the “sendEmail” method and adds the logging functionality. The plugin would then be called every time the “sendEmail” method is called, and the original method would still be executed as normal.

Here is an example of how the around plugin might be implemented:

public function aroundSendEmail(
    \Magento\Customer\Model\EmailNotificationInterface $subject,
    \Closure $proceed
) {
    // Log the email that is being sent
    $this->logger->info('Sending email to ' . $subject->getRecipientEmail());

    // Execute the original method
    $result = $proceed();

    // Return the result of the original method
    return $result;
}

Conclusion

Magento 2 plugins are a powerful tool for modifying the core functionality of the platform without changing the core code. Before plugins are used to modify the input parameters of a method before it is executed, and after plugins are used to modify the output of a method after it has been executed. Both types of plugins can be used to add new features or modify existing ones, making it easier to upgrade the platform and maintain stability.