Since its inception, Magento has been on the forefront in providing an open-source technology platform that allows merchants with eCommerce business to create and launch shopping cart systems, as well as control the general overview, functionality, and content of their online stores. For Magento to function as required, the presence of plugins is of the essence.

Plugins are software components that function by adding specific features to new or existing computer programs, with the sole purpose being to enhance customization and promote simplified operation. Plugins are of great importance to any eCommerce business because they can overcome issues such as system rewriting, facilitate the customization of similar methods but on different modules, and most importantly, they can help in preventing collision between different attributes of various plugins.

The process of creating plugins in Magento 2 is determined by various factors such as the function of the plugin, the type of the plugin created, and the restrictions that the plugin may be exposed to when installing it in the Magento 2 platform. Regardless of this, the process of creating plugins in Magento 2 is straightforward, and it is explained as follows.

In Magento 2, they are three different types of plugins that developers can create based on their preferences. These plugins are Before Listener Plugin, After Listener Plugin, and In Between Listener Plugin.

Before Listener Plugin

These types of plugins are used in instances where developers want to add new behaviors in a system or change different arguments present in a previously original method. For a developer to create this plugin, he must add the prefix “before,” to the name of the method, and set the first letter of the original method to Capital.

The first step in creating this plugin is by creating the di.xml at the Vendor/Module/etc/di.xml as the declaration plugin. This should be followed by the Productcart.php, which should act as the plugin class. The plugin class, on the other hand, should be created at Vendor\Module\Plugin\Productcart.php. The addProduct method should additionally be written as beforeAddProduct in the plugin class group.

After Listener Plugin

After Listener Plugins are used in instances where developers want to add different behaviors after calling the original method, or in instances where there is a need for change especially in the values returned by an original method. Unlike in the Before Listener Plugin, the After Listener Plugin is created by inputting the prefix “After” to the name of the method, while at the same time, setting the first letter of the original method to Capital. Another significant difference between the Before Listener Value and the After Listener Value is the fact that in the After Listener Value, a return value is not included in the creation process. The After Listener plugin can also be used as an AMP extension for Magento 2.

The first step in creating this plugin is by creating the Productname.php as the plugin class. This should be created at the Vendor/Module/Plugin/Productname.php. The getName method should be written as “afterGetName” in the plugin class. With this plugin example, it is easier for the developer to change the name of the product based on what has been designed, hence making it an ideal AMP extension for Magento 2.

In-Between Listener Value

With this plugin, around methods are defined based on their codes. What this means is that their codes are run before the method in question has been observed, and confirmed after the same method has been called. The sole purpose of observing the method before overrunning it is based on the fact that it allows the developer to override different prompts and methods. In this plugin creation method, an After plugin is used to change the returned values and arguments of an original method. Additionally, the plugin is used to add different plugin behaviors before and after the calling of the original method. The In-Between Listener Plugin is usually called by specifically including the prefix “Around” to the method name. This is then followed by adding the first letter of the previously original method to “Capital.”

The first step in this plugin creation method is to create the plugin declaration. In this case, the plugin declaration should be di.xml at vendor/module/etc/di.xml.

Create the Product.php as the plugin class at Vendor/Module/Plugin/Product.php. This should be followed by the addProduct method, which should be written as aroundAddProduct in the plugin class. By following these steps, the plugin return value should be formed in such a way that the present parameters should follow the $proceed parameter present in the around listener plugin.

The creation of plugins, as well as the application of Accelerated Mobile Pages extension for Magento 2, is a process that requires a perfect understanding of the plugins, as well as the function of the designed plugin. Based on the three types of plugins discussed, it is of the essence for developers to bear in mind the plugin created prior to starting the creation process. The notion behind this argument can be based on the fact that different plugins have different functionalities, and therefore, it is imperative to consider the function of the plugin or the application under which the plugin will function.

An important point to consider when creating plugins is the fact that if the plugin created was called by a method that matches the argument, then it must also match with the arguments, as well as simultaneously follow the method. Additionally, during the process, attention should be paid on the method’s original signature, the default parameters, and the suggestions available for use.

 

Spread the love