Skip to content

TrackingUrl #1147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gijsbertpieterbrouwer opened this issue Jan 27, 2025 · 1 comment
Open

TrackingUrl #1147

gijsbertpieterbrouwer opened this issue Jan 27, 2025 · 1 comment
Labels

Comments

@gijsbertpieterbrouwer
Copy link

The shopify documentation mentions you can retrieve the TrackingUrl for orderlines, this would be very helpful for me.
Is there any way to retrieve this with the ShopifySharp package?

@nozzlegear
Copy link
Owner

Hey! I can definitely add that to ShopifySharp, but you should also be able to grab it using the GraphQL API and ShopifySharp's GraphService right now if you're using the latest version of ShopifySharp. Here's how you'd do it:

var graphRequest = new GraphRequest()
{
    Query =
        """
        query getOrderWithTrackingUrls($orderId: ID!) {
            order(id: $orderId) {
                id
                name
                fulfillmentsCount {
                    count
                }
                fulfillments {
                    createdAt
                    deliveredAt
                    estimatedDeliveryAt
                    inTransitAt
                    updatedAt
                    displayStatus
                    id
                    requiresShipping
                    status
                    totalQuantity
                    trackingInfo {
                        company
                        number
                        url
                    }
                }
            }
        }
        """,
    Variables = new Dictionary<string, object>
    {
      {"id", $"gid://shopify/Order/{shopifyOrderId}"},
    }
};

var service = new GraphService(shopDomain, accessToken);
var result = await service.PostAsync<OrderWithTrackingUrlsQuery>(graphRequest);

That graph query will pull in a Shopify order along with all of its fulfillments/tracking info. You can check out Shopify's API docs for more info on what info you can grab with the order and fulfillment queries. ShopifySharp supports them all!

And here's a link to ShopifySharp's docs on using the GraphService to query and mutate Shopify's GraphQL API.


Deserializing the query above

If you use that query above, you need to create the OrderWithTrackingUrlsQuery class to deserialize it into:

public record OrderWithTrackingUrlsQuery(OrderWithTrackingUrls Order);

public record OrderWithTrackingUrls
{
    public required string Id { get; set; }
    public required string Name { get; set; }
    public required OrderFulfillmentsCount FulfillmentsCount { get; set; }
    public required IReadOnlyList<FulfillmentWithTrackingInfo> Fulfillments { get; set; }
}

public record OrderFulfillmentsCount(int Count);

public record FulfillmentWithTrackingInfo
{
    public required DateTimeOffset CreatedAt { get; set; }
    public required DateTimeOffset DeliveredAt { get; set; }
    public required DateTimeOffset EstimatedDeliveryAt { get; set; }
    public required DateTimeOffset InTransitAt { get; set; }
    public required DateTimeOffset UpdatedAt { get; set; }
    public required ShopifySharp.GraphQL.FulfillmentDisplayStatus DisplayStatus { get; set; }
    public required string Id { get; set; }
    public required bool RequiresShipping { get; set; }
    public required ShopifySharp.GraphQL.FulfillmentStatus Status { get; set; }
    public required int TotalQuantity { get; set; }
    public required FulfillmentTrackingInfo TrackingInfo { get; set; }
}

public record FulfillmentTrackingInfo
{
    public required string Company { get; set; }
    public required string Number { get; set; }
    public required string Url { get; set; }
}

Or you could skip writing your own classes and use ShopifySharp's pre-generated classes in the ShopifySharp.GraphQL namespace. If you do that, you only need to write one class to deserialize the result:

public record OrderWithTrackingUrlsQuery(ShopifySharp.GraphQL.Order Order);

All of that is explained in ShopifySharp's GraphQL docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants