Beginner’s Guide To Basic Plugin In NopCommerce 4.20
Introduction
One of the key features in nopCommerce is its pluggable architecture. Using plugins, it is very easy for anyone to add or extend features in nopCommerce without customizing its original source code.
NopCommerce has a vast range of plugins that are submitted by various nopCommerce vendors and nopCommerce teams which are available in nopCommerce marketplace.
This article is based on nopCommerce 4.20 and we are using Visual Studio 2019.
Plugin structure in nopCommerce
By default, all plugin projects with source code must be stored in \Plugins folder inside your nopCommerce application folder. It is a best practice to name a plugin project as “Nop.Plugin.{Group}.{Name}”.
A compiled plugin must be stored inside \Presentation\Nop.Web\Plugins folder. This is the location where nopCommerce looks for any plugins to load in its application on runtime.
Each plugin must have a “plugin.json” file inside its directory which is used by nopCommerce application to determine whether a plugin is compatible with the current version of nopCommerce and also other details regarding plugin as described in the table below.
A “logo.jpg” file can also be supplied with the plugin which is used to display a plugin picture wherever needed.
Please take a look at the below table to understand more about plugin.json file.
Both, plugin.json and logo.jpg, files should be marked as “Content” for “Build Action” and “Copy always” for “Copy to Output Directory” properties for both files inside your Visual Studio.
Steps to create a basic nopCommerce 4.20 plugin
Step 1
Locate nopCommerce 4.20 source code and double-click on “NopCommerce.sln” file to open NopCommerce project in your Visual Studio, as shown in the below image.
Step 2
Navigate to “Solution Explorer” and right-click on “Plugins” folder. Go to, Add > New Project.
Step 3
In “Add a new project” window, select “Class Library (.NET Core)” project and click on “Next”.
Step 4
Type a meaningful project name for your NopCommerce Plugin Project. We will use “Nop.Plugin.Misc.BasicPlugin” for this sample project, as shown in the below image.
Note
Your plugin project must be located inside the \Plugins folder in your nopCommerce project directory.
Step 5
(Optional) Right-click on your plugin project in Visual Studio and go to Properties. You can verify that “Target framework” has been set to “.NET Core 2.2”, as shown in below image.
Step 6
Open your plugin project .csproj file. Right-click on plugin project you just created and go to “Edit project file”.
Step 7
In your .csproj file, set “OutputPath”, “OutDir” and “CopyLocalLockFileAssemblies”.
Step 8
Right-click on your plugin project, go to Add > New item.
Step 9
Add a plugin.json file as shown in the below image.
Step 10
Right-click on plugin.json file that you just added and go to properties.
Step 11
In plugin.json file properties inside Visual Studio, set “Build Action” to “Content” and “Copy to Output Directory” to “Copy always”.
Step 12
Add contents to your newly added plugin.json file.
Note
You may want to refer to plugin.json file explained in the beginning of this article.
Step 13
Right-click on your plugin project and go to Add > Class.
Step 14
You can choose to set a meaningful name for the new class. In our case, we choose to set “BasicPlugin.cs” and click on the Add button.
Step 15
Your newly added class should look like the one shown in the below image.
Step 16
Right-click on “Dependencies” in your plugin project and click on “Add Reference”.
Step 17
In the “Reference Manager” window, go to Projects > Solution and select “Nop.Web.Framework” from list of available projects as shown in below image.
Note
You don’t need to add other projects separately after nopCommerce version 4.10 because the Nop.Web.Framework contains a reference to all other projects in the solution that come out of the box with the nopCommerce project.
Step 18
Once added Nop.Web.Framework in Dependencies, go to its properties and set “Copy Local” to “No”, as shown in below image.
Step 19
In the next step, inherit the class you added in step#14 with “BasePlugin” class.
Step 20
After you inherit “BasePlugin” class, write override for Install and Uninstall methods as shown below in your “BasicPlugin” class.
Step 21
Right-click on your project and build it as shown in below image.
Step 22
Run your project and login to /Admin area. Once you are logged in, go to Configuration > Local plugins as shown in below image.
Step 23
On the local plugins page, navigate to the plugin that you just created and click on the “Install” button as shown in below image.
Step 24
Once you have clicked on the “Install” button for your plugin, you will see buttons on top of local plugins page. Click on “Restart application to apply changes” to complete your plugin installation as shown in below image.
That’s it.
Conclusion
Now you have learned how to create a plugin from scratch in nopCommerce 4.20. Writing a plugin is really good when you are looking to upgrade nopCommerce and less expensive as well. But, it is expensive in terms of cost when you write a plugin compared to direct customization in nopCommerce source code.
We will cover other types of plugins that can be created in nopCommerce in our next article of the “Beginner’s Guide” series.
Article credits: Atul Rungta, Sandeep N. Chaudhary
Stay tuned..