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

Can't retrieve an order that uses a coupon

Open gaazkam opened this issue 4 years ago • 12 comments

Make sure you have included the below details when open an issue. Thank you.

  • Wordpress version, WooCommerce version and WooCommerce.NET version

Wordpress 5.7 (a XAMPP/Bitnami installation set up to test this issue), WooCommerce 5.1.0, WooCommerce.NET 0.8.3

  • Steps to replicate the issue
  1. Add a coupon (for the purpose of this test I added a coupon with the following settings: link to screenshot https://ibb.co/4fZTk4p )
  2. Make an order that uses this coupon, let's say the order's id is 62
  3. Attempt to retrieve this order with the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using WooCommerceNET;
using WooCommerceNET.WooCommerce.v3;

namespace ConsoleApp3
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var wc = new WCObject(new RestAPI
            (
                "http://localhost/wordpress/wp-json/wc/v3/",
                "redacted",
                "redacted",
                requestFilter: request =>
                {
                    request.UserAgent = "TestException";
                    request.Accept = "application/json";
                }
            ));

            var order = await wc.Order.Get(62);
            Console.WriteLine("done");

            Console.ReadLine();
        }
    }
}

The line var order = await wc.Order.Get(62) throws an exception.

  • Details of the error message if there is any
System.Runtime.Serialization.SerializationException
  HResult=0x8013150C
  Message=There was an error deserializing the object of type WooCommerceNET.WooCommerce.v3.Order. End element 'display_value' from namespace '' expected. Found element 'id' from namespace ''.
  Source=WooCommerce.NET
  StackTrace:
   at WooCommerceNET.RestAPI.DeserializeJSon[T](String jsonString)
   at WooCommerceNET.Base.WCItem`1.<Get>d__9.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ConsoleApp3.Program.<Main>d__0.MoveNext() in C:\Users\m\Documents\Bera\WTF\ConsoleApp3\ConsoleApp3\Program.cs:line 31

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
XmlException: End element 'display_value' from namespace '' expected. Found element 'id' from namespace ''.

gaazkam avatar Mar 22 '21 14:03 gaazkam

I have exactly the same issue...

WooCommerce.NET: There was an error deserializing the object of type WooCommerceNET.WooCommerce.v3.Order. End element 'display_value' from namespace '' expected. Found element 'id' from namespace ''. System.Private.DataContractSerialization: End element 'display_value' from namespace '' expected. Found element 'id' from namespace ''

nicolailund avatar Mar 23 '21 18:03 nicolailund

I have just tried with the "https://github.com/XiaoFaye/WooCommerce.NET/wiki/How-to-use-JSON.NET-in-WooCommerce.NET" to see if I got any more information. This is the error message:

Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Unexpected character encountered while parsing value: {. Path 'coupon_lines[0].meta_data[0].display_value', line 1, position 4748. Source=Newtonsoft.Json StackTrace: at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType) at Newtonsoft.Json.JsonTextReader.ReadAsString() 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.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) 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.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) 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.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonSerializer.Deserialize(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) at WooOrderUpdate.MyRestAPI.DeserializeJSon[T](String jsonString) in C:\Users\XYZ\source\repos\WooOrderUpdate\WooOrderUpdate\wooorderupdate.cs:line 32 at WooCommerceNET.Base.WCItem1.<Get>d__10.MoveNext()

This exception was originally thrown at this call stack: [External Code] WooOrderUpdate.MyRestAPI.DeserializeJSon<T>(string) in wooorderupdate.cs [External Code]`

nicolailund avatar Mar 23 '21 19:03 nicolailund

Try downgrading to v 0.8.2, this bug started happening for me after the latest release

Believe it was related to this fix: https://github.com/XiaoFaye/WooCommerce.NET/issues/523

Bobezlolzz avatar Mar 29 '21 09:03 Bobezlolzz

Try downgrading to v 0.8.2, this bug started happening for me after the latest release

Believe it was related to this fix: #523

I think it might be this one? #558

XiaoFaye avatar Mar 29 '21 09:03 XiaoFaye

I think it might be this one? #558

Highly likely, I noticed that retrieving orders which have custom meta_data JSON on them due to plugins will no longer deserialize into an order object since 0.8.3, haven't had time to investigate yet.

The woocommerce dynamic pricing plugin setting price on an order was causing the issue for me if you need to investigate!

Downgrading to 0.8.2 resolves the issue though

Bobezlolzz avatar Mar 29 '21 09:03 Bobezlolzz

@Bobezlolzz Do you mind sharing the json string so I can have a look? Set Debug = true and you will see it in the error message.

XiaoFaye avatar Mar 29 '21 09:03 XiaoFaye

@Bobezlolzz Do you mind sharing the json string so I can have a look? Set Debug = true and you will see it in the error message.

Forgive me I've never commented on git before, this is the json thats returning an error, I have no idea why, because it's valid.

https://i.imgur.com/6G0kp2Q.png

{"id":10000,"parent_id":0,"status":"completed","currency":"GBP","version":"5.0.0","prices_include_tax":false,"date_created":"2021-03-15T13:00:52","date_modified":"2021-03-15T15:38:03","discount_total":"0.00","discount_tax":"0.00","shipping_total":"10.50","shipping_tax":"2.10","cart_tax":"31.52","total":"201.72","total_tax":"33.62","customer_id":100,"order_key":"wc_order_null","billing":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"","email":"","phone":""},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"bacs","payment_method_title":"Direct bank transfer","transaction_id":"","customer_ip_address":"","customer_user_agent":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit\/605.1.15 (KHTML, like Gecko) Version\/14.0.3 Safari\/605.1.15","created_via":"checkout","customer_note":"","date_completed":"2021-03-15T15:38:03","date_paid":"2021-03-15T13:53:30","cart_hash":"null","number":"10000","meta_data":[{"id":1985295,"key":"is_vat_exempt","value":"no"},{"id":1985296,"key":"_yith_wcmc_show_checkbox","value":"1"},{"id":1985297,"key":"_yith_wcmc_submitted_value","value":"no"},{"id":1985301,"key":"_wcpdf_invoice_settings","value":{"enabled":"1","attach_to_email_ids":{"new_order":"1","customer_processing_order":"1","customer_completed_order":"1","customer_refunded_order":"1","customer_invoice":"1"},"display_shipping_address":"","display_phone":"1","display_customer_notes":"1","display_date":"invoice_date","display_number":"invoice_number","number_format":{"prefix":"","suffix":"","padding":""},"my_account_buttons":"available","use_latest_settings":"1","paper_size":"a4","font_subsetting":false,"header_logo":"28064","header_logo_height":"","shop_name":{"default":"null"},"shop_address":{"default":""},"footer":{"default":""},"extra_1":{"default":""},"extra_2":{"default":""},"extra_3":{"default":""}}},{"id":100000,"key":"_wcpdf_invoice_date","value":"1615813300"},{"id":100000,"key":"_wcpdf_invoice_date_formatted","value":"2021-03-15 13:00:53"},{"id":1985304,"key":"_wcpdf_invoice_number","value":"3931"},{"id":1985305,"key":"_wcpdf_invoice_number_data","value":{"number":3931,"formatted_number":"3931","prefix":"","suffix":"","document_type":"invoice","order_id":10000,"padding":""}},{"id":1985306,"key":"_new_order_email_sent","value":"true"},{"id":1985424,"key":"_wcpdf_packing_slip_date","value":"1615816402"},{"id":1985425,"key":"_wcpdf_packing_slip_date_formatted","value":"2021-03-15 13:53:22"},{"id":1985427,"key":"_wp_page_template","value":"default"}],"line_items":[{"id":27713,"name":"test","product_id":27761,"variation_id":0,"quantity":20,"tax_class":"","subtotal":"157.60","subtotal_tax":"31.52","total":"157.60","total_tax":"31.52","taxes":[{"id":1,"total":"31.52","subtotal":"31.52"}],"meta_data":[{"id":266914,"key":"_wcpdf_regular_price","value":{"incl":9.4600000000000009,"excl":7.8799999999999999},"display_key":"_wcpdf_regular_price","display_value":{"incl":9.4600000000000009,"excl":7.8799999999999999}},{"id":266916,"key":"_reduced_stock","value":"20","display_key":"_reduced_stock","display_value":"20"}],"sku":"SKU1","price":7.8799999999999999,"parent_name":null}],"tax_lines":[{"id":27715,"rate_code":"VAT-1","rate_id":1,"label":"VAT","compound":false,"tax_total":"31.52","shipping_tax_total":"2.10","rate_percent":20,"meta_data":[{"id":266915,"key":"_wcpdf_rate_percentage","value":"20.0000","display_key":"_wcpdf_rate_percentage","display_value":"20.0000"}]}],"shipping_lines":[{"id":27714,"method_title":"Next-Day Delivery","method_id":"flat_rate","instance_id":"1","total":"10.50","total_tax":"2.10","taxes":[{"id":1,"total":"2.1","subtotal":""}],"meta_data":[{"id":266907,"key":"Items","value":"test name","display_key":"Items","display_value":"test product"}]}],"fee_lines":[],"coupon_lines":[],"refunds":[],"date_created_gmt":"2021-03-15T13:00:52","date_modified_gmt":"2021-03-15T15:38:03","date_completed_gmt":"2021-03-15T15:38:03","date_paid_gmt":"2021-03-15T13:53:30","currency_symbol":"\u00a3","_links":{"self":[{"href":"https:\/\/www.example.co.uk\/wp-json\/wc\/v3\/orders\/10000"}],"collection":[{"href":"https:\/\/www.example.co.uk\/wp-json\/wc\/v3\/orders"}],"customer":[{"href":"https:\/\/www.example.co.uk\/wp-json\/wc\/v3\/customers\/100"}]}}

Bobezlolzz avatar Mar 29 '21 10:03 Bobezlolzz

Here's the error with debug = true:

System.AggregateException: 'One or more errors occurred.' Inner Exception Exception: There was an error deserializing the object of type WooCommerceNET.WooCommerce.v3.Order. End element 'display_value' from namespace '' expected. Found element 'incl' from namespace ''.

Bobezlolzz avatar Mar 29 '21 10:03 Bobezlolzz

Hi, same problem here. Occurs as soon as I updated to 0.8.3. Any more debug information needed?

dartrax avatar Apr 01 '21 11:04 dartrax

@XiaoFaye

Hi, Same problem here since last update (0.8.3)

display_value property should not be set to string, some display_value's from woocommerce are objects

TradePeg avatar Apr 10 '21 23:04 TradePeg

@TradePeg You are right, I will fix this in the next version.

XiaoFaye avatar Apr 11 '21 08:04 XiaoFaye

@XiaoFaye Thanks for this, any chance on getting 0.8.4 to nuget that includes this bug?

TradePeg avatar Oct 28 '21 11:10 TradePeg