WooCommerce.NET icon indicating copy to clipboard operation
WooCommerce.NET copied to clipboard

Get Specific Setting, Setting.Get()

Open nvsoares opened this issue 3 years ago • 2 comments

Hello.

I'm trying to get a setting by its Id, but the result is not the expected. I think the problem is at deserialization. Because Setting.Get([SettingId]) returns a Setting, but the server responds an array of settings. Furthermore, Setting class doesn't have all properties that are sent.

Can you help?

nvsoares avatar Apr 01 '21 13:04 nvsoares

Good day @nvsoares,

I think the problem is at deserialization. Because Setting.Get([SettingId]) returns a Setting, but the server responds an array of settings.

You are 100% correct with your assessment.

As a quick workaround for retrieving the settings for a specific id you could use an extension method to get the results you require:

    public static class WCObjectExtensions
    {
        public static async Task<List<SettingOption>> GetSettings( this WCObject wcObj, string id, Dictionary<string,string> parms = null )
        {
            return wcObj.Setting.API.DeserializeJSon<List<SettingOption>>( await wcObj.Setting.API.GetRestful( $"{wcObj.Setting.APIEndpoint}/{id}", parms ).ConfigureAwait( false ) );
        }

        public static async Task<SettingOption> GetSetting( this WCObject wcObj, string groupId, string settingId, Dictionary<string, string> parms = null )
        {
            return wcObj.Setting.API.DeserializeJSon<SettingOption>( await wcObj.Setting.API.GetRestful( $"{wcObj.Setting.APIEndpoint}/{groupId}/{settingId}", parms ).ConfigureAwait( false ) );
        }
    }

Usage:

  WCObject wc = new WCObject(.....); //Normal stuff here to create the WCObject

  var mySettings = await wc.GetSettings( "wc_admin" );
  // Or single setting
  var mySetting = await wc.GetSettting( "general", "woocommerce_store_address" );
  

firestormza avatar Apr 06 '21 22:04 firestormza

Good day @nvsoares,

Upon further investigation I see that these extension methods have already been implemented in the library.

Important: Reference the required extensions

...
using WooCommerceNET.WooCommerce.v3.Extension;
...

Usage:

WCObject wc = new WCObject(.....); //Normal stuff here to create the WCObject

var opts = await wc.Setting.GetSettingOptions( "general" );
//or
var opt = await wc.Setting.GetSettingOption( "general", "woocommerce_store_address" );

I'm not sure how to use the UpdateSettingOption() and UpdateSettingOptions() extension methods yet.

firestormza avatar Apr 06 '21 22:04 firestormza