Admin API does not return discount code when querying for an order

We are running a headless storefront with Shopify.

For the:

  • order confirmation page, and
  • account page - order details

we use the Admin GraphQL API to query for single order details. However, we’ve noticed that for orders with a discount code the code itself is not being returned by the API. I’ve tried both the discountCode and discountCodes fields but they just return null or an empty array.

We also query for totalDiscountsSet and successfully get the discounted amount - so the discount is valid and is being applied correctly.

The discount code is a normal one set up in the Shopify admin (not generated by a 3rd party app).

See screengrab for example of the API response for an order.

In the order summary for the admin the discount is just listed as Custom Discount, and does not list the code (I’m pretty sure that it used to?). See second screengrab.

As it happens, we also save the code in a custom attribute on the order (as we find that easier to pass through to our CRM), but am wondering why the native query is not working for us? Can anybody shed any light?

Thanks.

Hi @ManoMylv-BC

Did you try the property discountApplications https://shopify.dev/docs/api/admin-graphql/latest/queries/order#returns-Order.fields.discountApplications ?

Thanks for replying.

I didn’t do that, but, as far as I can see, there are no fields in that connection that actually return the discountCode (the fields are all about how the discounts are allocated, and their value).
I just need the code that the customer used (eg: DISCOUNT-123)

EDIT: Sorry, I didn’t read the thread properly.

@ManoMylv-BC , this is an interesting query.

  1. “discountCode” or “discountCodes” are returned for any discount codes applied to the items / the order. Here’s a screengrab of how it looks like when printing the order JSON from my admin. Here’s how it shows up on my side on an order that was placed with both product and order discounts applied.

  2. Discount codes applied on a line item level unfortunately, are not directly fetchable on the admin API as there are no references to it directly. However, there is an index filed in the “discountAllocations” → “discountApplication” → “index” that should point to the discount codes array and you should be able to know which discount is applied from the array (reference)

    Both these discounts are of the type “code” and are created directly in Shopify admin.

Can you check on your side on the order json and see if it reflects correctly there ?