Get an order's discount amount using Storefront API

I’m working on a JamStack eCommerce website using gatsby and Shopify. I have been able to fetch a customer’s orders using storefront API, the issue I’m facing is that I couldn’t get the discount amount on the order. (Custom discount) or any other discount.

I’m using this query to get orders

query GetCustomerPastOrders($customerAccessToken: String!){
    customer(customerAccessToken:$customerAccessToken) {
      orders(first: 100) {
        edges {
          node {
            ...
          }
        }
      }
    }
  }

But I couldn’t find a field on the storefront API that returns the discount amount. am I missing something?

Thanks in advance.

Hi @AbdallahAbis

I was able to find the discount amount as part of the line item in discountAllocations. Here’s an example -

{
  customer(customerAccessToken: "973532e1af12ceef4da6f614004340ff") {
    orders(first: 100) {
      edges {
        node {
          id
          lineItems(first: 20) {
            edges {
              node {
                discountAllocations {
                  allocatedAmount {
                    amount
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Thank you for your response @csam ,

unfortunately, I was looking to get the order-level discount.

I was able to do so by doing

{
  customer {
    orders {
      edges {
        node {
          discountApplications {
            edges {
              node {
                value {
                  ... on MoneyV2 {
                    amount
                  }
                  ... on PricingPercentageValue {
                    percentage
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

That does return the discount applied either if it’s a percentage or an amount, but the issue again is I can’t get the subtotal before any discounts/duties are applied. I used

currentSubtotalPrice => $36,540.00

it still includes the custom order-level discount in it even though the docs say :

Order.currentSubtotalPrice: MoneyV2!
The subtotal of line items and their discounts, excluding line items that have been removed. Does not contain order-level discounts, duties, shipping costs, or shipping discounts. Taxes are not included unless the order is a taxes-included order.

, normally it should return

$36,540.00 + $24,360.00 // should exclude the custom order-level discount

Raising this, since I don’t think you can get the discount code applied, just the value.

1 Like

@AbdallahAbis Here’s how you could calculate order level discount

const originalSubtotal = order.lineItems.edges
    .reduce(
        (partialSum, edge) =>
            partialSum + Number(edge.node.discountedTotalPrice.amount),
        0
    )
    .toFixed(2);

const orderLevelDiscount = (
    originalSubtotal - Number(order.subtotalPrice.amount)
).toFixed(2);