Skip to content

Plugin Configuration

Manage plugin settings and configuration in nopCommerce.

Settings Class

csharp
// MyPluginSettings.cs
using Nop.Core.Configuration;

public class MyPluginSettings : ISettings
{
    public bool Enabled { get; set; }
    public string ApiKey { get; set; }
    public int MaxItems { get; set; } = 10;
    public bool EnableLogging { get; set; }
}

Loading Settings

csharp
public class MyService
{
    #region Fields

    private readonly ISettingService _settingService;

    #endregion

    #region Ctor

    public MyService(ISettingService settingService)
    {
        _settingService = settingService;
    }

    #endregion

    #region Methods

    public async Task<MyPluginSettings> GetSettingsAsync()
    {
        return await _settingService.LoadSettingAsync<MyPluginSettings>();
    }

    #endregion
}

Saving Settings

csharp
[HttpPost]
public async Task<IActionResult> Configure(ConfigurationModel model)
{
    var settings = await _settingService.LoadSettingAsync<MyPluginSettings>();
    
    settings.Enabled = model.Enabled;
    settings.ApiKey = model.ApiKey;
    settings.MaxItems = model.MaxItems;
    
    await _settingService.SaveSettingAsync(settings);
    
    _notificationService.SuccessNotification("Settings saved");
    return RedirectToAction("Configure");
}

Store-Specific Settings

csharp
// Save for specific store
await _settingService.SaveSettingAsync(settings, storeId);

// Load for specific store
var settings = await _settingService.LoadSettingAsync<MyPluginSettings>(storeId);

Configuration Model

csharp
public class ConfigurationModel
{
    public int ActiveStoreScopeConfiguration { get; set; }

    [NopResourceDisplayName("Plugins.MyPlugin.Enabled")]
    public bool Enabled { get; set; }
    public bool Enabled_OverrideForStore { get; set; }

    [NopResourceDisplayName("Plugins.MyPlugin.ApiKey")]
    public string ApiKey { get; set; }
    public bool ApiKey_OverrideForStore { get; set; }
}

Default Settings on Install

csharp
public override async Task InstallAsync()
{
    await _settingService.SaveSettingAsync(new MyPluginSettings
    {
        Enabled = true,
        MaxItems = 10,
        EnableLogging = false
    });
    
    await base.InstallAsync();
}

public override async Task UninstallAsync()
{
    await _settingService.DeleteSettingAsync<MyPluginSettings>();
    await base.UninstallAsync();
}

Released under the nopCommerce Public License.