Magento 2 Plugins vs Observers vs Preferences: Which Should You Use?

Magento 2ArchitectureBest Practices

One of the most important decisions in Magento 2 development is how you change existing behaviour. Magento offers three main mechanisms: plugins (interceptors), event observers and preferences. Choosing the wrong one is a common cause of extension conflicts and painful upgrades. Here is how each works and when to use it.

Plugins (interceptors)

A plugin lets you run code before, after or around any public method of a class, without replacing the class. Plugins are declared in di.xml:

<type name="Magento\Catalog\Model\Product">
    <plugin name="vendor_product_name_suffix"
            type="Vendor\Module\Plugin\ProductNamePlugin"
            sortOrder="10"/>
</type>
class ProductNamePlugin
{
    public function afterGetName(Product $subject, ?string $result): ?string
    {
        return $result === null ? null : $result . ' (Special)';
    }
}
  • before methods can change the arguments passed to the original method.
  • after methods can change the return value.
  • around methods wrap the original call and decide whether to call it at all.

Limitations: plugins do not work on final classes or methods, non-public methods, static methods, constructors (__construct), or virtual types. Several plugins can target the same method; sortOrder controls their order.

Prefer before and after plugins. around plugins add overhead to every call and make it easy to accidentally skip the original method or other plugins. Use them only when you must control whether the original runs.

Event observers

Magento dispatches events at many points, for example sales_order_place_after, checkout_cart_product_add_after or customer_login. An observer reacts to an event without changing the code that dispatched it. Observers are declared in events.xml (globally, or in the frontend / adminhtml area):

<event name="sales_order_place_after">
    <observer name="vendor_notify_erp"
              instance="Vendor\Module\Observer\NotifyErp"/>
</event>
class NotifyErp implements ObserverInterface
{
    public function execute(Observer $observer): void
    {
        $order = $observer->getEvent()->getOrder();
        // queue the order for export...
    }
}

Limitations: you can only hook into events Magento (or another module) actually dispatches, and observers are meant for side effects; they cannot cleanly change a method's return value. Keep observers fast; push slow work such as API calls to a message queue.

Timing matters too. Many save events fire while the database transaction is still open, so the data can still be rolled back after your observer runs. For anything that talks to an external system, such as an ERP sync, Adobe's documentation recommends the *_commit_after events (for example catalog_product_save_commit_after), which fire only once the data is committed.

Preferences

A preference tells the object manager to use your class instead of another one everywhere it is requested:

<preference for="Magento\Catalog\Model\Product"
            type="Vendor\Module\Model\Product"/>

Preferences are the right tool for supplying an implementation of an interface (that is exactly how Magento wires its own service contracts). Using them to replace a concrete core class is risky:

  • Only one preference can win. If two extensions replace the same class, one silently breaks.
  • Your class inherits from the core class, so core changes in an upgrade can break it.
  • You take over far more behaviour than you usually need to change.

Side-by-side comparison

CapabilityPluginObserverPreference
Change arguments / return valuesYesNo (side effects)Yes
Multiple modules can coexistYes (sortOrder)YesNo, last one wins
Works on any public methodMost (not final/static/constructor)Only where an event is dispatchedWhole class
Upgrade riskLowLowHigh for concrete classes
Typical useModify a method's input or outputReact to something that happenedImplement an interface

A simple rule for choosing

  1. Reacting to something that happened (order placed, customer saved)? Use an observer, if a suitable event exists.
  2. Changing what a public method receives or returns? Use a before or after plugin.
  3. Need to decide whether the original method runs? Use an around plugin: sparingly.
  4. Providing an implementation of an interface? Use a preference.
  5. Thinking of a preference on a concrete core class? Look again for a plugin or observer first, and treat it as a last resort.

Following this rule is one of the main reasons well-built extensions can be installed side by side without conflicts. If you have inherited a store full of core rewrites, a Magento code audit will show where they are and how to replace them safely.

Sources

Sukumar Gorai

Sukumar Gorai

Adobe Certified Magento Expert and Technical Lead with 15+ years of experience and 62 extensions on the Adobe Commerce Marketplace. More about me →

Need help with this?

Independent code audits, architecture reviews, upgrade planning and technical leadership.

Code Audit & Consulting
Available for new projects

Have a Magento project in mind?

Tell me about your store and what you need. I reply to every enquiry within one business day.

Adobe Certified Expert

Adobe Commerce Developer certification, verified on Credly.

15+ years of Magento

From Magento 1 to Magento 2.4.x, Adobe Commerce and Hyvä.

62 Marketplace extensions

Each one reviewed and approved by Adobe.