Is Magento a good Ecommerce Platform?

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.

author avatar
Article by: Web Designers Near Me | Author: Joseph Cozens – Senior Website Designer

With over 25 years experience, Joseph started as the leased line manager for an ISP in the 1990’s and built websites for the support team internally, but was often asked by corporate business customers if we could provide a website for their company. So in 1998 with people looking for website designers near me, he started the web design company building websites part time, and in 1999 started building websites full time. In the early 2000’s we decided to call ourselves DesignsOnline.co.uk Joseph built one of the first online medical schools allowing doctors worldwide to enrol and pay for medical statistics training via the internet… Joseph used to build bespoke content management systems for companies who needed to regularly update their websites and for bespoke ecommerce websites which were hand coded at the time.

Today Joseph develops and builds top level professional websites without the needlessly high price tag.
You can contact Joseph directly via WhatsApp on 07944062954.

Your local reliable website designers

Need help? - Get a Quote in under a minute

Joseph & Stephanie Award Winning London Web Designers at The UK Web Design Company

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

Joseph & Stephanie web designers near you

Hi we are Stephanie & Joseph, we are a family company & we will personally be helping you with your website.
Fill in this quick & easy form in a few seconds
& we will contact you in less than 24 hrs with a quote
.
Is Magento a good Ecommerce Platform?

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.

author avatar
Article by: Web Designers Near Me | Author: Joseph Cozens – Senior Website Designer

With over 25 years experience, Joseph started as the leased line manager for an ISP in the 1990’s and built websites for the support team internally, but was often asked by corporate business customers if we could provide a website for their company. So in 1998 with people looking for website designers near me, he started the web design company building websites part time, and in 1999 started building websites full time. In the early 2000’s we decided to call ourselves DesignsOnline.co.uk Joseph built one of the first online medical schools allowing doctors worldwide to enrol and pay for medical statistics training via the internet… Joseph used to build bespoke content management systems for companies who needed to regularly update their websites and for bespoke ecommerce websites which were hand coded at the time.

Today Joseph develops and builds top level professional websites without the needlessly high price tag.
You can contact Joseph directly via WhatsApp on 07944062954.

Need help? - Get a Quote in under a minute

Local Friendly Web Designers - waiting to make you happy
Joseph & Stephanie web designers near you

Hi we are Stephanie & Joseph, we are a family company & we will personally be helping you with your website.
Fill in this quick & easy form in a few seconds
& we will contact you in less than 24 hrs with a quote
.

Need help? - Get a Quote in under a minute

London Web Site Designers Near Me

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

Joseph & Stephanie web designers near you

Hi we are Stephanie & Joseph, we are a family company & we will personally be helping you with your website.
Fill in this quick & easy form in a few seconds
& we will contact you in less than 24 hrs with a quote
.

See what people say about our web services