photo credit: Jigsaw via photopin (license)

Magento 2 : Why use rewrites when you can use plugins?

Remember class rewrites? That nifty little feature from Magento 1 where you could override any given class in Magento with your own implementation? Of course you do!

Well, then you also might remember the problem with rewrites: that as soon as more than 1 module tries to rewrite the same class you had a problem. In that case, you needed to add dependencies and rewrite the rewritten class (or even worse: edit community code so they would rewrite your rewritten class). Magento 2 also has rewrites. But it also has plugins. In this article I’ll cover both.

Class rewrites in Magento 2

If you want to rewrite a class in Magento 2, just like you would have done in Magento 1, you can do this in the file di.xml :

With this line, every time the Magento\Catalog\Model\Product -class is instantiated, Magento actually returns Gielberkers\Example\Rewrite\Magento\Catalog\Model\Product. The rest is the same as in Magento 1; you can make your new class extend the original class and rewrite some of the functions declared in the original class. You can even use constructor injection in your new class. However, like in Magento 1, it has the same big problem: it’s not very flexible when multiple models try to modify the behaviour of the same class.

And that’s where plugins come in!


Plugins are a concept in Magento that allow you to modify the behaviour of (almost) any given method in (almost) every class without having to rewrite them. Multiple vendors can register their plugins for the same class without having to worry about collisions. With a Magento plugin, you are able to do the following:

  • Manipulate the class or arguments before a method gets executed.
  • Manipulate the class or results of a method after it gets executed.
  • Perform actions ‘around’ the execution of a method (before and after in one method call).

So how does this actually work?

Register your plugin

Please note that i’ll be using the boilerplate module from the article ‘Creating a module in Magento 2‘ as the entry point for this article.

To register your plugin, we open our di.xml  file again and add the following line:

In the above we declared the following plugin for the type Magento\Catalog\Model\Product :

  • It has the name  ‘my_product_plugin’. This is actually arbitrary and it doesn’t follow any convention. It just should be unique.
  • type  points to the class in which our plugin resides.
  • sortOrder  can be used to give a priority. This way, multiple plugins can be registered for the same class, and you can control the order in which they are executed.
  • disabled  can be used if you want to disable your plugin, but why would you ever want to disable something cools like this?

Create the class

So lets just create our class shall we? In this example I follow the convention to keep the folder structure intact of the classes I write plugins for. I’m not sure if that’s the proper convention to follow, but it works for me. So in your folder where you store your plugin (Plugin/Magento/Catalog/Model  for example), create a file called Product.php  and add the following:

Now we have a very simple class but it immediately shows us some of the magic that is happening under the hood:

  • When a call is made to Product::setName($name) , the beforeSetName()  of our plugin is executed, making the $name -parameter lowercase.
  • Similar, when a call is made to Product::getName() , the result return from that method is passed to our plugins’ afterGetName() -method, and made uppercase in this example.
  • Last but not least, when a call is made to Product::save() , a call is made to the aroundSave() -method of our plugin that does something before and after the actual save-method. Note in the argument list of this method we have the callable argument $proceed . In this example this is the save-method of our product model, but with multiple plugins registered to the same class/method, this can also be the method of another plugin.

In conclusion

In Magento 1, a lot of times when you had to rewrite a class, this was mostly not because you drastically had to change the inner workings of a method, but in 95% of the cases you had to be able to manipulate the income or output of that specific method. With plugins you now have a more convenient and safer method to do such manipulations.

There will however still be situations where the rewriting of an entire class is desirable, but as with many things in Magento: when you want to achieve something there are more than often multiple ways in doing so.

This post is part of the series Magento 2 Development from Scratch.

Visitors give this article an average rating of 3.8 out of 5.

How would you rate this article?

One thought on “Magento 2 : Why use rewrites when you can use plugins?”

  1. Very Nice Article.

    Well Explained.

Leave a Reply