PHP / Magento Dev Blog

  • Publikacje
  • O autorze
  • Kontakt

Plugin vs Preference in Magento 2 – when to use which

by Henryk Tews / Tuesday, 11 September 2018 / Published in Magento 2

One of the most common questions when working with Magento 2 is: should I override a class with a preference, or use a plugin? The answer is almost always the same – use a plugin. But “almost” does a lot of work here. I explain the differences and show when a preference makes sense.

What is a Plugin (Interceptor)?

A plugin is an AOP (Aspect-Oriented Programming) mechanism in Magento 2. It lets you “wrap” an existing public method without overriding it. Three types available:

  • before – runs before the method, can modify arguments
  • after – runs after the method, can modify the result
  • around – takes full control, calls (or skips) the original method
<?php
namespace Vendor\Module\Plugin;

use Magento\Catalog\Model\Product;

class ProductNamePlugin
{
    public function afterGetName(Product $subject, string $result): string
    {
        return $result . ' - On Sale!';
    }
}

Registration in di.xml:

<type name="Magento\Catalog\Model\Product">
    <plugin name="vendor_module_product_name"
            type="Vendor\Module\Plugin\ProductNamePlugin"
            sortOrder="10"/>
</type>

What is a Preference?

A preference is a literal replacement of class A with class B in the DI container.

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

Why is a Plugin almost always better?

Key problem with preferences: there can only be one preference for a given class. If you and a third-party extension both set a preference for the same class – one silently wins. Plugins stack – all of them run, ordered by sortOrder.

When does a Preference make sense?

  1. You need to override a private or protected method – plugins only work on public methods.
  2. You need to change the constructor – plugins cannot modify __construct().
  3. You are implementing an interface differently
  4. The class is marked @api

Watch out for the “around” plugin

If you forget to call $proceed(), the original code and all queued plugins will never run. Use around only when you genuinely need to conditionally skip the original method.

Summary – a short rule

Before reaching for a preference, ask: would a plugin not be enough? In 90% of cases it is. A preference leaves behind a trail that is hard to debug during Magento upgrades.

About Henryk Tews

What you can read next

Xdebug – configuration, PHPStorm, debugging Magento plugins
Strategy pattern in PHP – and how Magento 2 uses it in pricing

© 2026 Created by

TOP
Zarządzaj zgodą
Aby zapewnić jak najlepsze wrażenia, korzystamy z technologii, takich jak pliki cookie, do przechowywania i/lub uzyskiwania dostępu do informacji o urządzeniu. Zgoda na te technologie pozwoli nam przetwarzać dane, takie jak zachowanie podczas przeglądania lub unikalne identyfikatory na tej stronie. Brak wyrażenia zgody lub wycofanie zgody może niekorzystnie wpłynąć na niektóre cechy i funkcje.
Funkcjonalne Always active
Przechowywanie lub dostęp do danych technicznych jest ściśle konieczny do uzasadnionego celu umożliwienia korzystania z konkretnej usługi wyraźnie żądanej przez subskrybenta lub użytkownika, lub wyłącznie w celu przeprowadzenia transmisji komunikatu przez sieć łączności elektronicznej.
Preferencje
Przechowywanie lub dostęp techniczny jest niezbędny do uzasadnionego celu przechowywania preferencji, o które nie prosi subskrybent lub użytkownik.
Statystyka
Przechowywanie techniczne lub dostęp, który jest używany wyłącznie do celów statystycznych. Przechowywanie techniczne lub dostęp, który jest używany wyłącznie do anonimowych celów statystycznych. Bez wezwania do sądu, dobrowolnego podporządkowania się dostawcy usług internetowych lub dodatkowych zapisów od strony trzeciej, informacje przechowywane lub pobierane wyłącznie w tym celu zwykle nie mogą być wykorzystywane do identyfikacji użytkownika.
Marketing
Przechowywanie lub dostęp techniczny jest wymagany do tworzenia profili użytkowników w celu wysyłania reklam lub śledzenia użytkownika na stronie internetowej lub na kilku stronach internetowych w podobnych celach marketingowych.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Zobacz preferencje
  • {title}
  • {title}
  • {title}