How to access custom settings in NopCommerce?

Custom settings in NopCommerce are great for including some custom functionality in your theme with minimal effort. In this tutorial, I'll tell you how you can create custom settings in NopCommerce and how you can access the setting values.

Custom settings in NopCommerce are great for including some custom functionality in your theme with minimal effort. In this tutorial, I'll tell you how you can create custom settings in NopCommerce and how you can access the setting values.

Creating Custom Settings

Custom settings are great way to store some user defined settings for your NopCommerce store. To create a custom setting: 1. Goto NopCommerce Administration and goto Configuration > Settings > All SettingsAll Settings Menu 2. Click on Add New Record button Add Record Button 3. Give your setting a name (the convention is to use <Setting Group Name>.<Setting Name> and click Update to save the setting. Give your setting a name and value Now that we have saved the setting, we need a way to access it in our view file.

Accessing the Custom Setting

In order to access this new setting in your theme, select the view where you wish to use the setting. Add this code in your view file.

var settingService = EngineContext.Current.Resolve<Nop.Services.Configuration.ISettingService>();
var settingValue = settingService.GetSettingByKey<string>("TestSetting.IsEnabled");

Let's understand this code. The first line instantiates ISettingService which is required for retrieving any setting from NopCommerce. Next we call GetSettingByKey method present in ISettingService interface. The method takes as parameter the name of the setting and returns the value of that setting. Now this value can be used anywhere in your view like:

if(settingValue == "True"){
  //do something if value is true 
} 
else{
  //do something if value is false 
}

 

Get updates directly in your inbox

My collection of ideas, thoughts, stories or anything straight from my heart.

Subscribe to Anshul Sojatia

Please provide your email address to subscribe or Login with Twitter to engage in conversation.