Push the limits of your Adobe Commerce store with MagePal Extension

Take your Adobe Commerce store to new heights with the power of MagePal extensions. Our expertly crafted code and rigorous testing ensure seamless integration with Adobe Commerce, giving you the freedom to focus on growing your business, not debugging. Upgrade your store's potential with MagePal.

Magento 2 Plugins - Interceptors

Plugins in Magento 2 are a powerful tool for customizing and extending the functionality of a Magento 2 store. They are also known as interceptors, and they allow developers to modify the behavior of existing classes within the Magento 2 codebase without having to change the underlying code. Plugins are a way to add custom logic to the existing functionality of a store, such as adding custom logic to a checkout process or modifying the behavior of a specific function.

There are three types of Magento 2 plugins: before, after, and around.

  1. Before plugins: These plugins are executed before the original method is called. They can be used to add validation logic or to change the input parameters of a method.

  2. After plugins: These plugins are executed after the original method is called. They can be used to add additional processing or to change the return value of a method.

  3. Around plugins: These plugins are executed before and after the original method is called. They can be used to add both pre-processing and post-processing logic to a method.

Before Plugins

Before plugins are used to modify the input data that is passed to a method before it is executed. For example, if you want to add a custom message to the checkout process, you can use a before plugin to modify the input data that is passed to the checkout method. To create a before plugin, you will need to create a new module in Magento 2 and then create a plugin that implements the \Magento\Framework\Interception\InterceptorInterface. The code for a before plugin might look something like this:

<?php
namespace Vendor\Module\Plugin;

class AddMessage
{
    /**
     * Add a message to the checkout process
     *
     * @param \Magento\Checkout\Model\Session $subject
     * @param string $message
     *
     * @return \Magento\Checkout\Model\Session
     */
    public function beforeAddMessage(
        \Magento\Checkout\Model\Session $subject,
        $message
    ) {
        $subject->addNotice($message);

        return $subject;
    }
}

In this example, the beforeAddMessage method is added to the checkout session class to add a message to the checkout process. The custom logic in this example simply adds a notice to the checkout session, but you could add any custom logic that you need to this method.

After Plugin

An after plugin allows you to add custom logic to a class method after the original method has been executed. This type of plugin is useful for adding post-processing logic to a method, such as logging, data analysis, or cleanup.

<?php
namespace Vendor\Module\Plugin;

class LogMessage
{
    /**
     * Log a message after it has been added to the checkout process
     *
     * @param \Magento\Checkout\Model\Session $subject
     * @param \Magento\Checkout\Model\Session $result
     *
     * @return \Magento\Checkout\Model\Session
     */
    public function afterAddMessage(
        \Magento\Checkout\Model\Session $subject,
        \Magento\Checkout\Model\Session $result
    ) {
        $message = $result->getMessages();
        // Log the message
        return $result;
    }
}

In this example, the afterAddMessage method is added to the checkout session class to log the message after it has been added to the checkout process. The custom logic in this example simply retrieves the message from the checkout session and logs it, but you could add any custom logic that you need to this method.

Around Plugin

An around plugin, also known as an around interceptor, is a type of plugin in Magento 2 that allows you to modify the behavior of an existing method by wrapping the original method with your own custom logic.

An around plugin is defined using the around keyword in the plugin class, followed by the name of the method that you want to modify. In the plugin method, you can call the original method using a callable, allowing you to change the behavior of the original method as needed.

The main advantage of an around plugin is that you can modify the behavior of an existing method without having to change the underlying code. This makes it easy to add custom functionality to an existing class, without having to modify the original codebase, which can make it easier to maintain and upgrade your code.

<?php
namespace Vendor\Module\Plugin;

class CustomPrice
{
    /**
     * Change the price of a product before it's added to the cart
     *
     * @param \Magento\Catalog\Model\Product $subject
     * @param callable $proceed
     * @param float $price
     *
     * @return float
     */
    public function aroundGetPrice(
        \Magento\Catalog\Model\Product $subject,
        callable $proceed,
        $price
    ) {
        // Get the original price
        $result = $proceed($price);
        // Modify the price
        $result = $result + 10;
        return $result;
    }
}

In this example, the aroundGetPrice method is added to the product class to change the price of a product before it is added to the cart. The custom logic in this example simply adds 10 to the original price of the product, but you could add any custom logic that you need to this method. The $proceed callable is used to call the original method, allowing you to change the behavior of the original method as needed.

Magento 2 / Adobe Commerce Extensions

0

Total Downloads

11

years experience with Magento

30+

Magento / Adobe Commerce Extensions

Shop with confidence

With millions of downloads worldwide, install with confidence knowing that our extensions will just work.

Learn More About MagePal Extension

Is your Magento store ready for the future? Say hello to the new Google Analytics 4, which is built from the ground up with all-new features and advanced machine learning technology.

GA4 Extension

Related

Magento 2 Extensions

Unleash the power of Magento 2 with MagePal extensions - A game-changer for your e-commerce business

DataLayer for Tag Manager

Whether you are integrating Bing, Facebook, SnapChat, Pinterest, or any other services our extension make it easy.

Enhanced Success Page

Add cross-sell, upsell, related products, social media links and other elements to your order confirmation.

HTML Minifier for Magento2

Minification is the process of removing all unnecessary characters and spacing from your source code without changing its functionality.