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

Once you’re started creating 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.

First, create an ISettings implementation in the root of the plugin.

public class MyPluginSettings : ISettings
{
    public string MySetting { get; private set; }
}

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.

Next, create the configuration model to be used on the configuration page – this will usually have the same values as the Settings object above:

public class ConfigurationModel {
    [NopResourceDisplayName("Name")]
    public string Name
}

Next, create the base plugin controller, which will hold the implementation for the configure page:

public class YourPluginController : BasePluginController
{
    [ChildActionOnly]
    [AdminAuthorize]
    [HttpGet]
    public ActionResult Configure()
    {
        // implementation..
    }

    [ChildActionOnly]
    [AdminAuthorize]
    [HttpGet]
    public ActionResult Configure(ConfigurationModel model)
    {
        // implementation..
    }
}

Finally, create the View.

Leave a Reply

Your email address will not be published. Required fields are marked *