
How to create a plugin in Magento 2
Creating a plugin in Magento 2 allows you to modify the behavior of existing class methods without directly modifying their code.
Here’s a step-by-step guide on how to create a plugin in Magento 2:
Step 1: Identify the Target Class
Before creating a plugin, you need to identify the class that you want to modify. This could be a core Magento class or a class from a custom module.
Step 2: Set up the Plugin Structure
Inside your Magento installation, navigate to the `app/code` directory. Create a new directory for your module using the following structure: `VendorName/ModuleName`. Replace `VendorName` with your desired vendor name and `ModuleName` with the name of your module.
Inside the module directory, create the necessary subdirectories: `etc` and `Plugin`. The `Plugin` directory will contain your plugin class, and the `etc` directory will contain the module’s configuration files.
Step 3: Create the Plugin Class
Inside the `Plugin` directory of your module, create a file with the naming convention `PluginName.php`. Replace `PluginName` with the desired name for your plugin. The file should contain a PHP class that implements the `Magento\Framework\Interception\InterceptorInterface` interface.
Here’s an example of a basic plugin class:
“`php
<?php
namespace VendorName\ModuleName\Plugin;
class PluginName
{
public function aroundMethodName($subject, $proceed, $arg1, $arg2)
{
// Perform your modifications here
// Call the original method
$result = $proceed($arg1, $arg2);
// Modify the result if needed
// Return the modified or original result
return $result;
}
}
“`
In the above example, replace `VendorName`, `ModuleName`, and `PluginName` with your own values. The `aroundMethodName` method is an example of an around plugin, which allows you to modify the input arguments, call the original method, and modify or intercept the result.
Step 4: Configure the Plugin
Inside the `etc` directory of your module, create a file named `di.xml`. This file is used to configure the dependency injection and specify the plugin configuration.
Add the following XML content to the `di.xml` file:
“`xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:ObjectManager/etc/config.xsd”>
<type name=”TargetClassName”>
<plugin name=”pluginName” type=”VendorName\ModuleName\Plugin\PluginName” sortOrder=”1″ disabled=”false” />
</type>
</config>
“`
Replace `TargetClassName`, `VendorName`, `ModuleName`, and `PluginName` with your own values. The `TargetClassName` should be the fully qualified class name of the target class that you want to modify.
Step 5: Enable the Module
To enable your module and activate the plugin, run the following command from the root directory of your Magento installation:
“`
php bin/magento module:enable VendorName_ModuleName
“`
Step 6: Apply Database Updates
If your module requires any database updates, run the following command to apply them:
“`
php bin/magento setup:upgrade
“`
Step 7: Test the Plugin
After creating and enabling the module, test the plugin by invoking the methods of the target class that you modified. The plugin will automatically intercept the method calls and execute the modifications you defined in the plugin class.
Congratulations! You have successfully created a plugin in Magento 2. You can now continue to enhance and expand the plugin functionality, by implementing other types of plugins (before, after) and modifying different class methods as needed.

Need help? - Get a Quote in under a minute

Stephanie & Joseph are Award Winning London Web Designers at The UK Web Design Company who are ready to help you with your website today.
Just take a couple of seconds to fill out this quick easy form and we will contact you right back
Need help? - Get a Quote in under a minute from the best web designers near you

How to create a plugin in Magento 2
Creating a plugin in Magento 2 allows you to modify the behavior of existing class methods without directly modifying their code.
Here’s a step-by-step guide on how to create a plugin in Magento 2:
Step 1: Identify the Target Class
Before creating a plugin, you need to identify the class that you want to modify. This could be a core Magento class or a class from a custom module.
Step 2: Set up the Plugin Structure
Inside your Magento installation, navigate to the `app/code` directory. Create a new directory for your module using the following structure: `VendorName/ModuleName`. Replace `VendorName` with your desired vendor name and `ModuleName` with the name of your module.
Inside the module directory, create the necessary subdirectories: `etc` and `Plugin`. The `Plugin` directory will contain your plugin class, and the `etc` directory will contain the module’s configuration files.
Step 3: Create the Plugin Class
Inside the `Plugin` directory of your module, create a file with the naming convention `PluginName.php`. Replace `PluginName` with the desired name for your plugin. The file should contain a PHP class that implements the `Magento\Framework\Interception\InterceptorInterface` interface.
Here’s an example of a basic plugin class:
“`php
<?php
namespace VendorName\ModuleName\Plugin;
class PluginName
{
public function aroundMethodName($subject, $proceed, $arg1, $arg2)
{
// Perform your modifications here
// Call the original method
$result = $proceed($arg1, $arg2);
// Modify the result if needed
// Return the modified or original result
return $result;
}
}
“`
In the above example, replace `VendorName`, `ModuleName`, and `PluginName` with your own values. The `aroundMethodName` method is an example of an around plugin, which allows you to modify the input arguments, call the original method, and modify or intercept the result.
Step 4: Configure the Plugin
Inside the `etc` directory of your module, create a file named `di.xml`. This file is used to configure the dependency injection and specify the plugin configuration.
Add the following XML content to the `di.xml` file:
“`xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:ObjectManager/etc/config.xsd”>
<type name=”TargetClassName”>
<plugin name=”pluginName” type=”VendorName\ModuleName\Plugin\PluginName” sortOrder=”1″ disabled=”false” />
</type>
</config>
“`
Replace `TargetClassName`, `VendorName`, `ModuleName`, and `PluginName` with your own values. The `TargetClassName` should be the fully qualified class name of the target class that you want to modify.
Step 5: Enable the Module
To enable your module and activate the plugin, run the following command from the root directory of your Magento installation:
“`
php bin/magento module:enable VendorName_ModuleName
“`
Step 6: Apply Database Updates
If your module requires any database updates, run the following command to apply them:
“`
php bin/magento setup:upgrade
“`
Step 7: Test the Plugin
After creating and enabling the module, test the plugin by invoking the methods of the target class that you modified. The plugin will automatically intercept the method calls and execute the modifications you defined in the plugin class.
Congratulations! You have successfully created a plugin in Magento 2. You can now continue to enhance and expand the plugin functionality, by implementing other types of plugins (before, after) and modifying different class methods as needed.
Need help? - Get a Quote in under a minute

Need help? - Get a Quote in under a minute

Stephanie & Joseph Award Winning London Web Designers at
The UK Web Design Company are ready to help you with your website
Just take a couple of seconds to fill out this quick easy form and we will contact you right back
Need help? - Get a Quote in under a minute from the best web designers near you