How can I retrieve discount details from an order using GraphQL?

How can I retrieve discount details from an order using GraphQL?

JackL
Shopify Partner
16 0 14

I have a discount that applied in a order and it showing this details in the order detail in admin panel

JackL_1-1695743373530.png

 

I was trying to retrieve this detail from the graphql, the following is the approach I made

        query ($numProducts: Int!, $cursor: String, $query: String!) {
          orders(first: $numProducts, after: $cursor, query: $query) {
            edges {
              node {
                createdAt
                lineItems(first: 20) {
                  nodes {
                    id
                  }
                }
              }
            }
            pageInfo {
              hasNextPage
              endCursor
            }
          }
        }

I not sure how do I get it from graphql, do anyone have idea?

Replies 3 (3)

dhull
Shopify Partner
2 0 0

The `order` object includes connections to (order-level) `discountApplications` and `lineItems`. I'm not sure where the information you highlighted in the screen capture came from, but the following query shows how you can retrieve both the order-level and lineitem-level discount allocations.

 

{
"query": "
query getOrder($orderId: ID!) {
order(id: $orderId) {
id customer { id } discountCodes
discountApplications(first: 5) { edges { node {
...discountApplicationFields
} } }
lineItems(first: 50) { edges { node {
id quantity currentQuantity name sku
discountAllocations {
allocatedAmountSet { shopMoney { amount currencyCode } }
discountApplication { ...discountApplicationFields }
}
} } }
}
}

fragment discountApplicationFields on DiscountApplication {
index
allocationMethod
targetType
value {
__typename
... on MoneyV2 { amount currencyCode }
... on PricingPercentageValue { percentage }
}
}
",
"variables":{"orderId":"gid://shopify/Order/12345"}
}

 The response will be like:

{
"data": {
"order": {
"id": "gid://shopify/Order/12345",
"customer": {"id": "gid://shopify/Customer/67890"},
"discountCodes": ["rwdlxjx9ak42l"],
"discountApplications": {
"edges": [
{
"node": {
"index": 0,
"allocationMethod": "ACROSS",
"targetType": "LINE_ITEM",
"value": { "__typename": "MoneyV2", "amount" : "20.0", "currencyCode": "USD" }
}
}
]
},
"lineItems": {
"edges": [
{
"node": {
"id": "gid://shopify/LineItem/111111",
"quantity": 2,
"currentQuantity": 1,
"name": "Widget - Deluxe",
"sku": "fbot-widget-002",
"discountAllocations": [
{
"allocatedAmountSet": {
"shopMoney": {"amount": "16.0", "currencyCode": "USD"}
},
"discountApplication": {
"index": 0,
"allocationMethod": "ACROSS",
"targetType": "LINE_ITEM",
"value": { "__typename": "MoneyV2", "amount" : "20.0", "currencyCode": "USD" }
}
}
]
}
},
{
"node": {
"id": "gid://shopify/LineItem/222222",
"quantity": 1,
"currentQuantity": 0,
"name": "Widget - Standard",
"sku": "fbot-widget-001",
"discountAllocations": [
{
"allocatedAmountSet": {
"shopMoney": {"amount": "4.0", "currencyCode": "USD"}
},
"discountApplication": {
"index": 0,
"allocationMethod": "ACROSS",
"targetType": "LINE_ITEM",
"value": { "__typename": "MoneyV2", "amount": "20.0", "currencyCode": "USD" }
}
}
]
}
}
]
}
}
}
}

 

ryan99
Shopify Partner
32 3 13

The problem with this is it doesn't seem to include any info about how the discount was applied (i.e. automatic vs code) or what discount was used (title, discountCode, id) Anybody found anything useful in the discountAllocations graphql?

dhull
Shopify Partner
2 0 0

You have to query for the __typename and the additional fields using standard GraphQL syntax. For example,

 

query getOrder($orderId: ID!) {
  order(id: $orderId) {
    id
    discountCodes
    discountApplications(first: 5) {
      pageInfo { hasNextPage endCursor }
      nodes { ...discountApplicationFields }
    }
  }
}

fragment discountApplicationFields on DiscountApplication {
  __typename
  index
  allocationMethod
  targetType
  targetSelection
  value {
    __typename
    ... on MoneyV2 { amount currencyCode }
    ... on PricingPercentageValue { percentage }
  }
  ... on DiscountCodeApplication { code }
  ... on AutomaticDiscountApplication { title }
}

and the response is like the following (note the "__typename": "DiscountCodeApplication" and "code": "rwdlv2qoxvf3w" in the response):

{
  "data": {
    "order": {
      "discountApplications": {
        "pageInfo": {
          "endCursor": "eyJsYXN0X2lkIjowLCJsYXN0X3ZhbHVlIjowfQ==",
          "hasNextPage": false
        },
        "nodes": [
          {
            "code": "rwdlv2qoxvf3w",
            "value": {
              "currencyCode": "USD"    ,
              "__typename"  : "MoneyV2",
              "amount"      : "20.0"
            },
            "index": 0,
            "allocationMethod": "ACROSS",
            "__typename": "DiscountCodeApplication",
            "targetSelection": "ALL",
            "targetType": "LINE_ITEM"
          }
        ]
      },
      "id": "gid://shopify/Order/5498811351191",
      "discountCodes": ["rwdlv2qoxvf3w"]
    }
  }
}