Magento Plugin Guide: What Is a Magento Plugin & How to Use It

What is a Magento Plugin?

A Magento plugin is a class that allows you to modify or extend the behavior of public methods in existing classes without altering core code. This uses Magento’s Interceptor Design Pattern, enabling safe and upgrade-friendly customization.

Plugins allow you to hook into a method call before, after, or around it, giving you control over input, output, or method logic itself.

Example: Imagine a traffic light controlling cars at an intersection. The traffic light follows a fixed sequence: green, yellow, red. Now, suppose you want to add a pedestrian crossing button that pauses the green light when pressed, letting people cross safely—without rebuilding or replacing the whole traffic system.

Magento plugins work like that pedestrian button: they let you insert extra steps before, after, or around existing actions in Magento’s system, customizing behavior without changing the original setup.

When Should You Use a Plugin?

  • To change the behavior of a public method.
  • To extend core functionality without overriding full classes.
  • To avoid conflicts seen with preferences or rewrites.

Avoid using plugins when:

  • You need to target non-public methods.
  • The same logic can be done using event observers.
  • Performance is critical, as plugins add overhead.

Types of Plugin Methods

  1. Before Method – Runs before the target method. Can change arguments.
  2. After Method – Runs after the method. Can change return value.
  3. Around Method – Wraps the method. Can override or skip it completely.

Step 1: Declare the Plugin in di.xml

<type name="Magento\Customer\Model\Customer">
    <plugin name="custom_plugin" type="Vendor\Module\Plugin\CustomerPlugin" sortOrder="1" />
</type>

Step 2: Create the Plugin Class

<?php

namespace Vendor\Module\Plugin;

class CustomerPlugin
{
    public function beforeSetFirstname(\Magento\Customer\Model\Customer $subject, $firstname)
    {
        $firstname = ucfirst(strtolower($firstname));
        return [$firstname];
    }

    public function afterGetFirstname(\Magento\Customer\Model\Customer $subject, $result)
    {
        return strtoupper($result);
    }

    public function aroundSave(\Magento\Customer\Model\Customer $subject, \Closure $proceed)
    {
        // Custom logic before save
        $result = $proceed();
        // Custom logic after save
        return $result;
    }
}

Best Practices for Using Plugins

  • Use only when needed: Excess plugins affect performance.
  • No circular dependency: Avoid chaining preferences and plugins on same class.
  • Target concrete classes: Don’t use interfaces/abstract classes.
  • Test edge cases: Plugins affect base logic—test thoroughly.

Conclusion

Magento plugins let you customize behavior in a precise and maintainable way. When used wisely, they offer flexibility without core code changes. But overuse or misuse can lead to performance issues and unexpected results.

Interested in learning more? Check out more blogs in Tech Solutions for related insights and tips.