Adding Settings to a Plugin in NopCommerce (pre-4.00)

Once you’re created a plugin for NopCommerce, you’ll likely want to add the ability to configure settings inside the plugin for reference later.

When adding this capability, we’re going to work on trying to make this as immutable as possible, to follow functional programming as best we can, just because it makes things a little cleaner and puts all of the conversion between the configuration model and settings object.

Locale Keys

First, we’ll set up a class called MyPluginLocaleKeys.cs that holds the different locale keys used to represent the settings descriptions in the Configure page:

Configuration Model

Next, we’ll create a configuration model named Models/MyPluginConfigModel.cs that represents the configure page for the plugin. This will get populated by the plugin’s settings, and then be responsible for receiving user data and saving the setting values provided.

In addition to usually having a 1:1 relationship to the plugin settings, you can also provide DataAnnotations that handle model validity and display names. When providing a NopResourceDisplayName, we’ll use the locale key provided above.

Settings Object

Next, create an ISettings implementation in the root of the plugin named MyPluginSettings.cs.

This is going to work a little differently – we’re going to create this to be immutable, so we’ll both initialize it from a config model, create a “default” settings object, and allow for easily converting to a config model.

We use private set here to make sure we initialize the settings object from a configuration model (when saving) and disallow the ability to change the settings object – making it immutable.

Base Plugin Class

Next, create the base plugin class (named MyPluginPlugin.cs, so for example, EnhancedLoggingPlugin.cs, used for defining the type of plugin and defining the install/uninstall options. We’ll do a few special things

Configure Controller and View

The final step is creating both the Configuration controller and view to finish the ability to view and save plugin settings data. Create the Controllers/MyPluginConfigController.cs file:

Then create the Views/Configure.cshtml file: