A to Z About Magento 2 Plugins – Put It In Your Pocket

by Anna Nguyen

.Start with Magento, you might have dozens of questions about it. Besides the common keywords that are easy-to-meet like Magento 2 extensions, Magento 2 modules, Magento 2 apps, etc… you could have seen “Magento 2 plugins”. 

Although it’s not new, there are some things that can make you confused. In this blog, we will clear all common questions about what is plugins for Magento 2. Let’s read and understand that.

1. What Are Magento 2 Plugins?


Magento 2 plugin, or interceptors, is a class that alters the behavior of public class functions by intercepting function calls and executing code before, after, or around them. For every class or interface, this allows you to replace or extend the functionality of original, public methods.

The plugin is pretty similar to an extension or module. Both are added to the default Magento and often developed by the third party. 

This interceptor method helps to avoid conflicts among extensions that can happen when changing the behavior of the same class or function.

By using plugins for Magento 2, you can change the behavior of a class without having to update the class itself.

2. How Many Types Of Magento 2 Extension?

There are 3 types of Magento 2 add-on: Before Plugin, After Plugin, Around Plugin.


  • Before method is run first before the call to an observed method. Note that you must name these methods the same as the observed method with the prefix ‘before’.

By returning a modified argument, you can utilize before methods to modify the arguments of an observed method. The method should return an array of parameters if there are any. It should return a null result if the method does not change the parameter for the observed method.

  • After method is run after the observed method is completed. These methods must be named after the observed procedure and have the prefix ‘after’.

By altering the initial result and returning it at the end of the method, you can change the outcome of an observed method.

Note: If the observed method declares a parameter to be optional, the after method should do the same.

  • Around method is run before and after its observed method. The name of the around method must be the same as the observed method, with the prefix ‘around’. 

You can override an observed method by using these methods.

Note: When not essential, avoid utilizing around method plugins because they raise stack traces and have a negative impact on performance.

3. The Elements Of Plugin

To start writing a plugin, you need to know how many elements there are to create a plugin and how to use them. Read the explanation below to more fully understand:


Plugin example


  • Type name: a class or interface that the plugin observes.
  • Plugin name: An arbitrary name for a plugin that identifies it. Also used to integrate the plugin’s configurations.
  • Plugin type: The name of a plugin’s virtual type or class. When specifying this element, use the following naming convention:

Extra elements that you can use:

  • sortOrder: Sort order will be used to run plugins that call the same method.
  • Disable: Set this element to true to make a plugin deactivated. False is the default setting.

4. Several plugins for Magento 2 Examples

With 3 types of interceptors in Magento 2, we give 3 examples for each type below.


The code without a plugin

Before plugin example:


* Modify name before get hello message


* @param Index $subject

* @param string $name

* @return array


public function beforeGetHelloMessage(

Index $subject,

string $name

): array {

$name = 'ltc';

return [$name];


Around plugin example:


* Check and modify name and msg after call proceed


* @param Index $subject

* @param callable $sproceed

* @param string $name

* @return mixed


public function aroundGetHelloMessage(

Index $subject,

callable $proceed,

string $name


if ($name === ‘ltc') {

$name = ‘lucthanca’;


$result = $proceed($name);

sresult['msg'] .= " Welcome to BssCommerce!";

return $result;


After plugin example:


* Add more text in message


* @param Index $subject

* @param array $result

* @param string $name

* @return array


public function afterGetHelloMessage(

Index $subject,

array $result,

string $name

): array {

$result['msg'] .= " How are you today?”";

return $result;


5. Plugins Vs Observers Vs Preferences In Magento 2 – Differences

a. What is the difference between observers and plugins in Magento 2?

Firstly, you need to know what Observers in Magento are. Observers are a sort of Magento class that can change business logic, influence general behavior, and improve performance. Observers are activated whenever the event manager dispatches the event they’ve been configured to watch.


Besides the common point that both plugins and observers can be utilized to run our custom script, after certain Magento 2 – events or public methods are executed. We can compile all the main differences below:

  1. Observers can edit both private and protected methods, but plugins can only modify public methods.
  2. Although there is a sort order for plugins, there is none for observers.
  3. Only events that have already been delivered in Magento can be added to the observer list. In this case, plugins are more flexible.
  4. Plugins are required when we need to change Magento core functionality (for example, adding more data to the order collection object). We can’t use Observer in this instance.
  5. Because Magento creates at least three objects in response to a trigger event, observers are slower than plugins.

b. Magento 2 difference between plugin and preference

The Object Manager uses preference to specify the default implementation. It can be used to rewrite a class from another module to point at the implementation, making the class global. You can use the preference from di.xml to override a public or protected method from a core class. The default implementation is defined by the preference node.


The basic and simple way to distinguish between Magento 2 plugins and preferences is: Preference is used for overriding class, Plugin is used for adding functionality before, after, and around methods.

6. What Are The Limitations Of Plugin Magento 2?

Although Plugin in Magento 2 is flexible and useful in many cases, it also has limitations that you should notice. You shouldn’t use Magento 2 plugin on the following:

  • Final methods
  • Final classes
  • Non-public methods
  • Class methods (such as static methods)
  • __construct and __destruct
  • Virtual types
  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • Objects that implement Magento\Framework\ObjectManager\NoninterceptableInterface

7. 3+ Suggestions Of Magento 2 Plugins For Your Website

a. Magento 2 SEO Extension

One of the most common ways to measure website quality is by tracking the site ranking on Google. So, hundreds of thousands of businesses have focused on optimizing search engines. This way benefits both businesses’ reputation and revenue.


However, optimizing your website the traditional way without a support tool is not easy. You should “put in your pocket” Seo extension for Magento 2 to save both time and effort.

This is a tool kit that contains 10+ functions that make your website friendly with users and step closer to Top 1 in SERPs.

This tool kit maximizes your SEO effectively through the factors such as Rich Snippets, Advanced Breadcrumbs, HTML Sitemap, XML Sitemap, Robot Meta Tag, Canonical Tag, HREFLANG Tag, 301 Redirects, Meta Tag Template, Image ALT Tags, SEO External Links, SEO Toolbar and SEO Report and Audit.

b. Magento 2 B2B Registration Form

For store owners to understand business characteristics, manage long-term relationships, and even offer relevant promotion programs, wholesale client data is critical.

Furthermore, B2B registration verification is a must! There is a chance that some of the signups are spam, garbage accounts, or firms that do not satisfy your criteria.

Don’t worry because Magento 2 B2B Registration Form will resolve the above problem. 


You can classify B2C and B2B customer groups by using personalized sign-up forms for them. This plugin will allow you to: create a separate registration form for B2B customers and control B2B registration: preview, pending, approve or reject, and more and more features.


c. Magento 2 Checkout Custom Field

Have you ever thought about how to collect customer information to explore their insights? From that, you can easily create effective marketing plans and take care of each customer group better. You can see the increasing customer value and your revenue.

Adding a new field on the Checkout page is the solution I want to mention. And many merchants have found it on Google. 


It’s very simple, you just add Magento 2 Checkout Custom Field to your website. All the features you need will be shown. Add any number of Magento 2 custom fields, custom fields should appear in the Order grid and on the Order Detail page and more.

Install now to expand your customer data and be ready to generate outstanding plans!

Furthermore, you can refer to 101+ Free Magento 2 Extensions or explore 148+ optimization plugins Magento 2 to make your website the best.

Wrap Up

Above is the list of common questions that users need to know and clear when starting researching Magento 2 add-on. Hope you all can grab that basic knowledge to have the foundation to develop more and more on the Magento market.

BSS Commerce is one of the leading Multi-platform eCommerce solution and web development services providers in the world. With experienced and certified developers, we commit to bring high-quality products and services to optimize your business effectively.

FIND OUT our full Magento Website Development services here.

NEW: We have launched Trello’s Product Roadmap to keep you updated on our latest product versions. Share your feedback and suggestions for Magento solutions to help us improve your experience here. With our Product Roadmap, you can also request updates or compatibility for your desired modules or suggest a new one to enhance your website.
Explore NOW!

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.