What's the most reliable way to fetch a line item's unit discount?

For example, imagine that I’m creating an order in Shopify’s Admin portal. I add a line item with 5 quantity and I enter a $0.50 discount for the line. While I entered $0.50 and the UI displays $0.50, the full discount amount is actually $2.50 because the line has 5 quantity, which is fine so far.

When I load the order and its line via GraphQL, various line discount fields report that the discount amount is $2.50. Which makes sense, but what if I want the “unit discount” of $0.50? What’s the most reliable way of getting it? I’m aware that doing some basic math is simple at first, but it’s not as simple once you start factoring in the possibility that the order uses a different presentment currency than the shop currency, since now there’s rounding error and the total discount amount might not be cleanly divisible by the line’s quantity.

Additionally, we’ve encountered issues where certain discount types will be reflected on some line fields such as discount allocations, but not the TotalDiscountSet. So I’m curious about that too.

1 Like

Hey again @Jonathon_Minard :waving_hand:

The easiest way to grab a line item’s per-unit discount would likely be with a query like this:

query GetOrderLineItemUnitDiscounts($orderId: ID!) {
  order(id: $orderId) {
    lineItems(first: 10) {
      edges {
        node {
          id
          quantity
          originalUnitPriceSet {
            shopMoney {
              amount
            }
          }
          discountedUnitPriceAfterAllDiscountsSet {
            shopMoney {
              amount
            }
          }
        }
      }
    }
  }
}

discountedUnitPriceAfterAllDiscountsSet should show the final price of a unit after considering all discounts, including line-item, order-level, and code-based discounts.

I can’t say for sure, but for the TotalDiscountSet issue you mentioned, the likely reason wsome discounts weren’t applying may be because TotalDiscountSet doesn’t include order-level discounts (more info in our docs here).

Hope this helps - let me know if I can clarify anything on my end here.