Shopify GraphQL Discounts: How to Get Exact Money Amounts (Not Just Percentages!)
Hey there, fellow store owners and developers!
Ever found yourself diving into the Shopify Admin API, specifically GraphQL, trying to pull out every last detail about an order, only to hit a snag when it comes to discount codes? You want to see the exact money amount of each discount applied, just like you might see in a webhook payload, but GraphQL seems to be playing coy, offering up percentages or just the code strings instead of hard cash values.
That's exactly what came up in a recent community discussion, and it's a fantastic example of how diving into the nuances of Shopify's API can save you a lot of headaches. Let's unpack what our community expert, Robert_Kanaan, shared to clarify this common challenge.
The Discount Dilemma: REST Webhooks vs. GraphQL
Our community member, Tom_Raney, kicked off the conversation, pointing out a key difference. When an order updates, the REST webhook for discount_codes sends a beautiful, clear array:
"discount_codes": [
{
"amount": "10.00",
"code": "discounttest",
"type": "fixed_amount"
},
{
"amount": "19.99",
"code": "foo",
"type": "percentage"
}
]
See that? Exact amount values for each discount! Super helpful for reporting, integrations, or just understanding your order economics.
But then Tom tried to get the same info via GraphQL, and things looked a little different. The discountCodes array in GraphQL was just a list of strings, and discountApplications only showed the percentage, not the actual monetary value:
{
"index": 1,
"allocationMethod": "ACROSS",
"__typename": "DiscountCodeApplication",
"value": {
"percentage": 10.0,
"__typename": "PricingPercentageValue"
},
"targetType": "LINE_ITEM",
"targetSelection": "ALL",
"code": "foo"
}
Frustrating, right? You're left wondering, "Is there a way to extract the value for each discount in the GraphQL query without calculating it myself?" And then there's the inevitable follow-up: "If I do calculate it, how does Shopify handle rounding? 10% of 199.98 is 19.998, but Shopify shows 19.99. What's the rule?"
Unlocking Exact Discount Values: Meet discountAllocations
This is where Robert_Kanaan stepped in with some truly golden advice. He quickly clarified that if you're looking for the actual calculated money amount of a discount, discountCodes and discountApplications aren't quite what you need. Think of it this way:
discountCodes: Just the code strings themselves (e.g., "SUMMER20").discountApplications: Describes the rule or intent of the discount (e.g., "20% off line items," "fixed amount of $10"). It tells you how the discount should be applied.discountAllocations: This is the hero! It represents the actual amount discounted on a specific line item or shipping line, after all the rules, calculations, and rounding have been applied by Shopify.
So, to get that webhook-style result with the exact amount for each discount, you need to query the discountAllocations on both lineItems and shippingLines, and then sum them up.
Your Step-by-Step Guide to Querying Discount Amounts
Let's get practical. Here's how you can construct your GraphQL query to fetch those precise discount amounts using discountAllocations. This example targets a specific order by its ID:
query OrderDiscounts($id: ID!) {
order(id: $id) {
id
name
discountCodes
currentTotalDiscountsSet {
shopMoney {
amount
currencyCode
}
}
lineItems(first: 100) {
nodes {
id
name
discountAllocations {
allocatedAmountSet {
shopMoney {
amount
currencyCode
}
}
discountApplication {
index
targetType
value {
__typename
... on PricingPercentageValue {
percentage
}
... on MoneyV2 {
amount
currencyCode
}
}
... on DiscountCodeApplication {
code
}
}
}
}
}
shippingLines(first: 25) {
nodes {
title
discountAllocations {
allocatedAmountSet {
shopMoney {
amount
currencyCode
}
}
discountApplication {
index
targetType
value {
__typename
... on PricingPercentageValue {
percentage
}
... on MoneyV2 {
amount
currencyCode
}
}
... on DiscountCodeApplication {
code
}
}
}
}
}
}
}
A quick note: Robert reminds us that if an order has more than 100 line items, you'll need to paginate the lineItems query to ensure you get all the allocations. The same applies to shippingLines if you somehow have more than 25 (though that's less common).
Summing It Up: Getting Your Total Discount Per Code
Once you have the data from the query above, you'll need to do a little local processing to consolidate the amounts per discount code. The key is to sum the allocatedAmountSet.shopMoney.amount from both lineItems and shippingLines. You can group these by:
discountApplication.index(a unique identifier for each discount application within the order)- Or, more specifically for code-based discounts, by
discountApplication.code
Robert tested this approach against a real order, and the results were spot on. The sum of the GraphQL allocations matched the REST/webhook-style discount_codes.amount exactly (e.g., 23.06 + 7.68 = 30.74).
The Rounding Question: Trust Shopify's Finalized Amounts
Remember Tom's question about rounding (10% of 199.98 becoming 19.99, not 20.00)? Robert's advice here is gold: don't try to recreate Shopify's calculation or rounding rules yourself.
The beauty of using discountAllocations is that these fields already contain Shopify's finalized result. This includes everything: entitlement rules, allocation method, line splitting, quantity handling, shipping/line targeting, and crucially, currency rounding. Shopify calculates and rounds at a granular level, ensuring consistency across the platform. By relying on allocatedAmountSet.shopMoney.amount, you're getting the exact, official number Shopify uses.
So, there you have it! When you need to understand the true monetary impact of each discount code in your Shopify orders via GraphQL, skip the discountCodes strings and discountApplications rules. Go straight for discountAllocations.allocatedAmountSet.shopMoney.amount on your lineItems and shippingLines. It's the most reliable way to get those precise, Shopify-validated discount values, saving you from complex calculations and rounding guesswork. Happy querying!