A plugin, or “interceptor” in Magento 2, is an essential customization component that allows developers to extend and modify the behavior of a class’s public methods without directly editing its source code. In other words, a plugin acts as an intermediary, intercepting and modifying method calls without altering the original class’s source code.

Plugins can be set to execute before, after, or around an existing method, offering significant flexibility to modify Magento’s behavior without touching the core code. They are a key element of Magento 2’s architecture, allowing developers to customize the platform cleanly and structurally.

1. Your first encounter

Plugin and observer—observer and plugin. They seem similar, do similar things, but their usage isn’t exactly the same! Don’t smash your keyboard just yet; by the end of this article, everything will be clearer!

Both plugins and observers modify behavior, hence the confusion. At first glance, they seem identical: they both listen to native behavior and add your custom code. But there’s more!

The logic behind each is different: plugins hook into methods, whereas observers hook into events.

You might already see things more clearly now, but let’s go further: when should you use one or the other?

2. Observer: the classic publish-subscribe

Observers rely on events dispatched by Magento (you can also create your own events, but that’s for another article).

With an observer, you don’t alter existing behavior; instead, you add parallel processing.

Here’s a practical example:

You’re asked to create a module that automatically assigns a customer group for new customers registering with an email ending in @acme.fr. You would hook into the customer_register_success event to perform this operation. You’re not interfering with the existing flow; you’re just adding an extra step—observing and reacting accordingly.

Implementation is straightforward:

  • Declare the event: Create an etc/events.xml file in your module and hook into customer_register_success.
  • Create the observer class: Implement \Magento\Framework\Event\ObserverInterface. Write your logic in the execute() method to change the customer group.
  • Retrieve data and apply logic: Receive an Observer object $observer. From here, fetch what you need using $observer->getData() or, typically in this scenario, $observer->getCustomer().

3. Plugin: the art of intercepting a method

Plugins don’t rely on events; they directly intercept method execution. Basically, you’re saying: “before this method runs, I want to do something,” or “once it’s done, I want to modify the result,” or even “I’m taking complete control.”

Magento supports three plugin types:

  • before → Executes your code before the method, allowing argument modifications.
  • after → Executes your code after the method, allowing return value modifications.
  • around → Gives you full control, deciding whether to call the original method or not.

Here’s a concrete example:

You want to automatically alter a customer’s name when it’s loaded (perhaps adding a [VIP] tag). Here, we’re targeting a specific method: \Magento\Customer\Api\Data\CustomerInterface::getFirstname.

You’ll create a plugin applying to this method, returning a modified version of the firstname. Unlike observers, plugins intervene directly in the execution flow, giving you much more control—and responsibility.

Implementation steps:

  • Declare your plugin in di.xml: Create etc/di.xml and specify:
    • Target class (the one you intercept)
    • Your plugin class name
    • Plugin type (before, after, around)
  • Create your plugin class: No interface needed; define a method like afterGetFirstname, beforeSave, or aroundExecute based on the plugin type. Magento executes this automatically.
  • Apply your logic: Receive arguments (in before), result (in after), or callable (in around). You return the proper values or call $proceed() if using around.

Simple in concept yet powerful. But since you’re injecting yourself into Magento’s native flow, you need to be meticulous—a poorly written plugin can quickly become a nightmare to maintain.

4. Observer vs Plugin: who wins when?

An observer is like a trusty Swiss army knife. It adds functionality without altering core behaviors. You hook into a specific Magento cycle moment (an event), do your thing, and let the system continue unaffected. You don’t change method arguments or results. You observe, act beside the flow, and don’t break anything—clean, straightforward, readable.

A plugin is your surgical tool. Need to intervene in a method? Before, after, or even replace it completely? That’s your tool. But with great power comes greater risk—poor implementation, plugin stacking, side effects. You can easily complicate your code unless you clearly define limits.

In short:

  • If adding parallel processing without touching native flow → Observer.
  • If intercepting a specific method to alter its data, arguments, or result → Plugin.
  • To block or fully redirect method execution → Plugin (around), but beware, it’s the trickiest to maintain.

There’s no universal solution. The key is understanding exactly what you want and choosing the simplest, clearest, most robust approach. Unsure? Start with an observer. You can always switch to a plugin later if needed.

5. Conclusion: Quick recap

  • Observer: You watch what’s happening and add your touch alongside, without changing the main flow.
  • Plugin: You dive into the guts. You intervene in methods, before, after, or in place of. More powerful but riskier.

When in doubt, always start with an observer—simpler, clearer, and usually enough.

If your requirement is ultra-specific, targeting a particular method and you want to modify its return or parameters → plugin, no hesitation.

Above all: code for others (or your future self in six months). The clearer the code, the healthier it stays.

And if you ever regret your choice… remember, goat farming in Larzac remains a viable alternative.

observer_en