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, etc… you could have seen “Magento 2 plugins”. Although it’s not new, there are some things that can make you confused. In this Magento tutorial, we will clear all common questions about “What is plugin in Magento 2? Stay tuned!
What is Plugin in Magento 2: The Definition
Table of Contents
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 are often developed by a 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 Magento 2, you can change the behavior of a class without having to update the class itself.
Extensions are developed through the vast network of Magento partners to provide you with the flexibility and ability to maintain your store by the way you want.
Besides, the Magento community provides us with a great variety of modules both free and paid ones, available for downloading and installing from the Magento Marketplace catalog, and third-party developers’ websites such as BSSCommerce, AheadWorks, Amasty, and so on.
Together with the rich Magento default functionality, the Magento 2 extensions allows us to solve many tasks without programming skills.
How Many Plugin Types Are Available in Magento?
There are 3 types of Magento 2 add-ons: Before Plugin, After Plugin, Around Plugin.
#1. Before Plugin
Before method is run, the call to an observed method. Note that you must name these methods the same as the observed method with the prefix ‘before’.
You can use before methods to change the arguments of an observed method by returning a modified argument. An array of parameters (if there are any) should be returned by the method. If the method doesn’t change the parameter for the observed method, it will return a null result.
#2. After Plugin
After the observed method is completed, the after method will start running. They must be named after the observed method with the prefix ‘after’
You can change the outcome of an observed method by modifying the initial result and returning it after the method ends.
Note: If the observed method declares a parameter to be optional, the after method should do the same.
#3. Around Plugin
Around method starts running before and after its observed method. Around method has the same name as the one of the observed methods, with the prefix ‘around’
Please note that an observed method can be overridden with these methods.
Note: When not essential, avoid utilizing around method plugins because they raise stack traces and have a negative impact on performance.
The Elements Of Plugin
To start writing a plugin, you need to know how many elements there are to conduct Magento 2 create plugin and how to use them. Read the explanation below to more fully understand:
- 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:
\Vendor\Module\Plugin\<ClassName>
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.
Several Plugins for Magento 2 Examples
With 3 types of interceptors in Magento 2, we give 3 examples for each type below:
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; }
Plugins Vs Observers Vs Preferences In Magento 2 – Differences
#1. 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:
- Observers can edit private and protected methods, but plugins can only modify public methods.
- Although there is a sort order for plugins, there is none for observers.
- Only events that have already been delivered in Magento can be added to the observer list. In this case, plugins are more flexible.
- 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.
- Because Magento creates at least three objects in response to a trigger event, observers are slower than plugins.
#2. 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, and the plugin is used for adding functionality before, after, and around methods.
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
Where to Buy Magento 2 Extensions?
There are many options out there. In this article we will show you two of the best marketplaces.
#1. Magento Marketplace
It is always the first choice of both developers and store owners because it is the official Magento extension store and the global eCommerce resources to apply and serve Magento solutions with new features and functionalities.
The official Magento 2 extension store provides sellers with a simplified user experience to quickly discover the products and services in high quality to extend the functionalities of their Magento stores and to deliver engaging experiences to their customers.
Furthermore, the marketplace offers extension developers access to a significant and growing customer base. The Magento Marketplace introduces a new vetting process that includes a technical, marketing, and business value review, which ensures that merchants only buy high-quality technologies from the most trusted developers in the Magento ecosystem.
#2. BSS Commerce
BSS Commerce is one of the best Magento extension providers that develops a variety of modules for Magento 2 including free and paid extensions.
Magento default has some cons for both admin and customers, BSS Commerce provides users with the best Magento extensions with high quality to help build your Magento stores better as well as improve the drawbacks of default Magento and enhance the user shopping experience.
By installing BSS Commerce Magento 2 extensions, you can easily configure your online Magento store from the admin panel and save much time for finding free codes on the Internet. Also, you will have a better user experience for your store, and your products will become more competitive and get more sales.
Many of BSS Commerce Magento 2 extensions are reviewed on Magento Marketplace, and thousands of customers around the world chose and trusted these extensions.
What is Extension in Magento 2: 3+ Suggestions For Your Website
#1. 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 to users and steps closer to the Top 1 in SERPs.
This tool kit maximizes your SEO effectively through 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.
#2. 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.
#3. 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.
>>> Furthermore, you can refer to 101+ Free Magento 2 Extensions or explore 148+ optimization plugins for Magento 2 to make your website the best.
Wrap Up: What is Plugin in Magento 2?
Above is the list of common questions that users need to know and clear when starting to research Magento 2 plugins. BSS Commerce hopes that you all can grab that basic knowledge to have the foundation to develop more and more on the Magento market.
When it comes to Magento development service, BSS Commerce is committed to delivering the best solution to your business. We are a top-tier Magento 2 development agency with more than 11 years of experience in the eCommerce industry. Throughout nearly 1500 development projects, we have satisfied over 100,000 customers around the globe with our exceptional service quality!
If you are looking for an unparalleled solution to maximize your Magento store’s sales growth, don’t hesitate to contact us now!