nozzlegear / ShopifySharp

ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
https://nozzlegear.com/shopify-development-handbook
MIT License
742 stars 309 forks source link

How can I get the 'current_quantity' of 'line_items' from Order #975

Closed oono-webshark closed 7 months ago

oono-webshark commented 9 months ago

Hi,

I would like to obtain the 'current_quantity' of 'line_items' from the response that can be retrieved under 'Retrieve a specific order' on the following page. How can I go about doing this?

https://shopify.dev/docs/api/admin-rest/2024-01/resources/order#get-orders-order-id?fields=id,line-items,name,total-price

nozzlegear commented 9 months ago

Hey @oono-webshark! I thought we had support for this already, but it looks like I was thinking of something else. After looking at the docs, it turns out this property is only available in version 2024-01 of Shopify's API, while ShopifySharp still uses version 2023-07. We'll likely be updating to 2024-01 once it releases in January, but if you don't want to wait you should be able to override the API version with your own custom order service class:

public class CustomShopifyOrderService : OrderService
{
  public override string APIVersion => "2024-01";

  public CustomShopifyOrderService(string shopDomain, string accessToken) : base(shopDomain, accessToken)
  {
  }

  public async Task<OrderWithCustomLineItem> GetWithCustomLineItemAsync(long orderId, string fields = null, CancellationToken cancellationToken = default)
  {
      return await ExecuteGetAsync<OrderWithCustomLineItem>($"orders/{orderId}.json", "order", fields, cancellationToken);
  }
}

public class OrderWithCustomLineItem : ShopifySharp.Order
{
  [JsonProperty("line_items")]
  public new IEnumerable<CustomLineItem> LineItems { get; set; }
}

public class CustomLineItem : ShopifySharp.LineItem
{
  /// The line item's quantity, minus the removed quantity.
  [JsonProperty("current_quantity")]
  public int CurrentQuantity { get; set; }
}

Let me know if that works for you!

oono-webshark commented 9 months ago

Hi, @nozzlegear

Thank you for your kind response. I tried the sample code you provided, but when I executed GetWithCustomLineItemAsync, an exception occurred. The error message seemed to be a 404.

Due to my lack of skills, I couldn't find a solution. As there's no rush, I'm thinking of waiting for the January release. If I have time, I'll try to work with the code you provided again.

oono-webshark commented 7 months ago

I tryed to do above sample code you write. It work well. Thank you for your support.