What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Need Nested GraphQL Json String for Tracking Numbers

Solved

Need Nested GraphQL Json String for Tracking Numbers

jhartwell
Shopify Partner
34 1 15

Need some help with a JSON String using GraphQL. I need a GraphQL JSON string to Search a Shopify users past purchases by Customer.ID and return back the following:

  • Order ID
  • Product SKU
  • Tracking Numbers
  • Financial Status
  • Paid Status

 

How do I add the Product SKU and Tracking to this:

{
  orders(first: 5, reverse:true, query: "customer_id:6886949683441" ) {    edges {
      node {
        name
        id
        displayFulfillmentStatus
        displayFinancialStatus
      }
    }
  }
}

 

Accepted Solution (1)

ShopifyDevSup
Shopify Staff
1453 238 525

This is an accepted solution.

Hi @jhartwell 👋

 

For a given customer's orders, you can use the `Customer.orders` connection. To build a query for orders, please review the API reference documentation for the `Order` object and add the appropriate fields and connections. Below is an example that queries all requested fields:

{
    customer (id: "gid://shopify/Customer/6886949683441") {
        orders (first:10) {
            nodes {
                name
                id # Order ID
                displayFulfillmentStatus
                displayFinancialStatus
                fullyPaid # Paid Status
                lineItems (first:10) {
                    nodes {
                        variant {
                            sku # SKU
                        }   
                    }
                }
                fulfillments {
                    trackingInfo {
                        number # Tracking Numbers
                    }
                }
                transactions {
                    status # Financial Status
                }
            }
        }
    }
}


Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 8 (8)

ShopifyDevSup
Shopify Staff
1453 238 525

This is an accepted solution.

Hi @jhartwell 👋

 

For a given customer's orders, you can use the `Customer.orders` connection. To build a query for orders, please review the API reference documentation for the `Order` object and add the appropriate fields and connections. Below is an example that queries all requested fields:

{
    customer (id: "gid://shopify/Customer/6886949683441") {
        orders (first:10) {
            nodes {
                name
                id # Order ID
                displayFulfillmentStatus
                displayFinancialStatus
                fullyPaid # Paid Status
                lineItems (first:10) {
                    nodes {
                        variant {
                            sku # SKU
                        }   
                    }
                }
                fulfillments {
                    trackingInfo {
                        number # Tracking Numbers
                    }
                }
                transactions {
                    status # Financial Status
                }
            }
        }
    }
}


Hope that helps!

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

jhartwell
Shopify Partner
34 1 15

This is an acceptable solution...Wow thanks so much. I'm still learning how to use GraphQL and this worked perfectly. Let me do some more testing, before I mark it as done.

jhartwell
Shopify Partner
34 1 15

Ok, everything shows up, but the Tracking Numbers do not correlate to the actual Product line items. As you can see from the attached image. Node 1 (Red) Variant corresponds to Node 3 fulfillments. Is there a way to make the nodes correspond directly to the variants? The same with the blue. Varient 2 (blue) shows the tracking number of fulfillments 1, so it'll be hard for me to correlate the two.

 

Correlation.png

ShopifyDevSup
Shopify Staff
1453 238 525

Hey @jhartwell

 

Great question. There are a few ways you can tackle this. One way would be to use the FulfillmentLineItem object instead to get the line items associated with fulfillment. That would group the fulfillment tracking  with the line items. 

Another way you could look at it as well is to query the orders and filter by customer. This may help better organize the query. An example like this: 

 

{
 orders(first: 10, query: "customer:6886949683441") {
   nodes {
     name
     fulfillments {
       fulfillmentLineItems(first: 5) {
         nodes {
           lineItem {            
             sku
           }
         }
       }
       trackingInfo {
         number
       }
     }
   }
 }
}

 

As you're learning, the graphiQL app is going to be a great resource to test out different queries and formats to build this in the way that will work best for you. 

- https://shopify.dev/docs/apps/tools/graphiql-admin-api 

We also have some really great posts and accompanying videos in our Partners Blog on Getting started with GraphQL 

- https://www.shopify.com/partners/blog/getting-started-with-graphql 

Hope this helps get you on the right track!

 

-Kyle

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

jhartwell
Shopify Partner
34 1 15

This was an acceptable solution. I'm able to use my Integromat Iterator tools to group everything how I wish. Thanks!

DaveBitton
Shopify Partner
11 0 1

How would you query for a specific customer's specific order? I have some customers with MANY orders. Thanks,

ShopifyDevSup
Shopify Staff
1453 238 525

Hey @bittondb

 

If you know the specific order, you could query the order object for the order Id. If you need to retrieve the order through the customer object though, you can narrow down the orders returned by using one of the available filters: 

 

You can see the available filters here: https://shopify.dev/docs/api/admin-graphql/2023-10/objects/Customer#connection-customer-orders

 

Hope that helps. 

 

- Kyle G.

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

DaveBitton
Shopify Partner
11 0 1

What I really need is the purchased_product() function. So, for a given customer and product, did they buy it, and if so, show a badge on a product image. this method in liquid doesn't work for a customer with many many orders:

 

assign purchased_product = customer.orders | map: 'line_items' | where: 'product_id', product.id
 
so, what shall I do?