WooCommerce.NET
WooCommerce.NET copied to clipboard
error on GetSalesReport
WordPress 5.6.2 WooCommerce.NET 0.8.3 WooCommerce 5.0.0
Hi I found a problem "Could not convert string to integer: 0.00. Path '[0].total_discount'" during use function getsalesreport, could you please help? How can I do?
my code: public async Task<List<SalesReport>> GetDataSaleReport() { List<SalesReport> report = new List<SalesReport>(); string period = "last_month"; string minDate = "2021-02-04"; string maxDate = "2021-02-04"; Dictionary<string, string> periods = new Dictionary<string, string>(); //periods.Add("period", period); periods.Add("date_min", minDate); periods.Add("date_max", maxDate); report = await wc.Report.GetSalesReport(periods);
return report;
}
error: {Newtonsoft.Json.JsonReaderException: Could not convert string to integer: 0.00. Path '[0].total_discount', line 1, position 182. at Newtonsoft.Json.JsonReader.ReadInt32String(String s) at Newtonsoft.Json.JsonTextReader.FinishReadQuotedNumber(ReadType readType) at Newtonsoft.Json.JsonTextReader.ReadNumberValue(ReadType readType) at Newtonsoft.Json.JsonTextReader.ReadAsInt32() at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) ...
Good day, I see there is a problem. I've submitted a pull request to fix it. #598 I see the totals property is also not being de-serialized correctly, so for now I guess you'll have to do it yourself if you need the data.
As a quick workaround you could use the jsonDeserializeFilter Func on the RestAPI
object constructor to deserialize the object yourself.
Here are some code samples I use in my project:
public static class WooApiService
{
public static WCObject GetWooApi( Func<string, string> jsonSerializeFilter = null, Func<string, string> jsonDeserializeFilter = null )
{
// Get the API using stored variables - you'll use your own here
// Pass along the filters
return new RestAPI( AppGlobal.dbConfig.WooApiUrl, AppGlobal.dbConfig.WooApiKey, AppGlobal.dbConfig.WooApiSecret, AppGlobal.dbConfig.WooUseBasicAuth, jsonSerializeFilter, jsonDeserializeFilter );
}
}
The to get the sales report:
JObject objYear = null;
MyNewShinyObject myNewShinyObject = null;
var wcYear = WooApiService.GetWooApi( null, json =>
{
// Here you can deserialize the json string with anything or any objects you like
myNewShinyObject = JsonConvert.DeserializeObject<MyNewShinyObject>( json );
// or
// return a generic JObject to use later on
var raw = JsonConvert.DeserializeObject( json );
if ( raw is JArray jArr && jArr.HasValues && jArr[0] is JObject )
objYear = (JObject)jArr[0];
// Return an empty object array so that the call does not fail, the result from the "GetSalesReport" call will be an empty List<SalesReport> object because of this
return "[]";
} );
await wcYear.Report.GetSalesReport( new Dictionary<string, string> { { "period", "year" } } );
if ( objYear is not null )
{
// Access the data
string totalDiscountString = (string)objYear["total_discount"];
// Here you can use the JObject in any way you like (See Newtonsoft JSON docs)
new FrmJsonViewer( objYear, "Year Object" ) { MdiParent = this }.Show();
}
// OR
if (myShinyNewObject is not null)
//use object that you deserialized yourself
Hi firestormza, Thank you for your suggestion, I'm trying by follow your code :)