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 13

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?

Reply 1 (1)

dhull
Shopify Partner
1 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" }
}
}
]
}
}
]
}
}
}
}