Discount codes not applied to draft order calculate & create

Hi.

How do I apply discount codes when I calculate and create draft orders?

I’m using the GraphQL api to calculate and create a draft order. I want to apply discount codes while doing so, and capture the discounted line item price.

The problem is that the discount code I pass to CalculateDraftOrder is not applied, and I don’t get any user errors back.

I have set the discount code to be active in the sales channel name for my app in Shopify Admin. I have also tried with discount codes that apply to all customers and all products. The app (api) credentials allow for order creation, reading and writing of discount codes.

Do I need to pass more parameters for this to work? Any specific requirements on the products configurations for discounts to work (e.g. sales channels enabled)? Any more configurations or credentials needed for the app/api?

It the same when I create a draft order and pass a discount code. It won’t be applied.

------ Code ------

const draftOrderInput = {
lineItems: lineItems,
discountCodes: [“DISCOUNT2025”],
acceptAutomaticDiscounts: true,
allowDiscountCodesInCheckout: true,
};

const draftOrderMutation = mutation CalculateDraftOrder($input: DraftOrderInput!) { draftOrderCalculate(input: $input) { calculatedDraftOrder { discountCodes lineItems { variant { id } name originalUnitPrice { amount currencyCode } approximateDiscountedUnitPriceSet { shopMoney { amount currencyCode } } } } userErrors { field message } } } ;

const draftOrderResponse = await shopify.graphql(draftOrderMutation, {
input: draftOrderInput,
});

------ Response ------

{
“draftOrderCalculate”: {
“calculatedDraftOrder”: {
“discountCodes”: ,
“lineItems”: [
{
“variant”: { “id”: “gid://shopify/ProductVariant/22312123123123” },
“name”: "XXX ",
“originalUnitPrice”: { “amount”: “35625.0”, “currencyCode”: “SEK” },
“approximateDiscountedUnitPriceSet”: {
“shopMoney”: { “amount”: “35625.0”, “currencyCode”: “SEK” }
}
}
]
},
“userErrors”:
},
“severity”: “INFO”,
“message”: "Draft order calculation response: "
}

Hey @Daniel_Alsen :waving_hand: - the mutation there looks like it’s formatted correctly, but based on the response, it looks like the discountCode from the input itself might not be passing over correctly.

Would you be able to share an X-Request-ID value from the API Response headers we send out for an example where we’re seeing that empty discountCodes response output? I can take a look at our logs to see if we can replicate things on our end here.

Hope to hear from you soon!

Hi Alan!

I only get the body back in my graph ql-client, so I would have to modify that to get the response header.

Can you see the query from tha logs using our store name “luftix” and the api request time?

2025-04-12T14:25:25.346552502Z

Best,

Daniel

Hi, Daniel
I’d like to ask about the specific definition of this discountCodes: [“DISCOUNT2025”],. Is it possible that this draft order does not meet the usage conditions?

Hi.

I added all possible flexibility for the code, and I also tested the code on the shop website. It works there on the shopping basket and check-out.

I implemented my own calculate draft order function for now, loading all discount codes and applying them manually to the line items, and this works. But it would be better to be able to use the built-in api function.

Best,

Daniel

Hey @Daniel_Alsen - thanks for sharing that timestamp and the store name - are you using the Shopify GraphiQL app or your own GQL client within your app? If it’s within your own app, if you’re able to share the name of the app as it appears in your shop’s admin, I should be able to use that to pull the info in our logs. I think that’s the last thing I’d need though, hope to hear from you soon!

Hi Alan!

Thanks for your help.

I’m using shopify-api-node to connect to Shopify and get the graphQL client. Then it’s just regular GraphQL calls to your apis after that, I’m not using the REST api or functions in shopify-api-node, except getting the client.

https://www.npmjs.com/package/shopify-api-node

All other api calls are working well, it’s just the discount code that is not picked up on the order calculation mutation. Again, I’m using

The store name is “luftix” and the app name is “installer-portal” (221293445121).

https://admin.shopify.com/store/luftix

https://admin.shopify.com/store/luftix/settings/apps/development/221293445121/overview

I made multiple calls to the order calculation api, but one was around:

2025-04-12 T 14:25:25

Best,

Daniel

1 Like

Thanks Daniel - I did some more digging into this and it does seem odd that the discount code in your example from the 12th didn’t work as it does seem to apply to all the products in the collection that the discount code is set up for.

I’ll do some more digging into this on our end here and speak with the team to see if this is potentially something on our end - I’ll loop back with you once I have more info!

Hey @Daniel_Alsen - I was able to do a bit more looking into this on our end here and it looks like the specific discount code there was tied to a specific collection and all of the assigned products/variants were correctly allocated to that specific collection so that was all good, but it looks like the discount also only applied to a specific customer segment as well.

I dug into that a bit further too, and it seems like only one customer is assigned to that segment (customer ID: 7731497730222). The example from April 12th didn’t include an assigned customer ID under the purchasingEntity object - including valid customers IDs for a customer segment assigned to a discount should then let the discount apply though (just sharing this link here for reference: draftOrderCalculate - GraphQL Admin). This should solve the issue.

Hope this helps - let me know if I can clarify anything on our end!

Thanks for the digging Alan, you are spot on :blush:

I set the discount to apply to all customers and did a few api calls this morning, with the same result. Still no codes applied and no user error messages. Maybe you can take another look at the logs?

Maybe it’s so that the order calculation api has to be called with a customer ID in all cases, for discounts to apply? It could perhaps be good to add a comment on that in the order calculation api documentation for the discount codes parameter, in that case?

I will try to supply the customer ID and switch to that approach. Two questions then:

How would you modify my query in that case, to include the customer ID, for the discount codes to be picked up?

How can I do the query so the VAT is not automatically added per line item?

All the best,

Daniel

Hey Daniel - no worries!

You should be able to get this set up like this:

mutation CalculateDraftOrder($input: DraftOrderInput!) {
  draftOrderCalculate(input: $input) {
    calculatedDraftOrder {
      taxLines {
        title
        rate
      }
      lineItems {
        title
      }
      customer {
        id
        taxExempt
      }
    }
    userErrors {
      field
      message
    }
  }
}

Variables:

{
  "input": {
    "billingAddress":  {
      "address1": "input-here",
      "city": "input-here",
      "provinceCode": "input-here",
      "zip": "input-here"
    },
    "shippingAddress": {
      "address1": "input-here",
      "city": "input-here",
      "provinceCode": "input-here",
      "zip": "input-here"
    },
    "purchasingEntity": {
      "customerId": "gid://shopify/Customer/input-here"
    },
    "taxExempt": true,
    "lineItems": [
      {
        "quantity": 1,
        "variantId": "gid://shopify/ProductVariant/input-here"
      }
    ]
  }
}

By setting the customer ID in the purchasingEntity value array there, it should assign that particular customer to the calculated draft order, and as long as you’ve set up the customer to be tax exempt, it should carry over to the draft order calculation (more info on that here).

Let me know if you encounter any other blockers on this though - happy to keep digging if needed! :slight_smile:

Hi Alan!

Thank you so much for your help.

It works now when I submit the purchasing entity.

One final question:

Is there a way to create a draft order so the prices display excluding VAT per line item, and that there is total VAT line at the bottom?

When I set taxExempt = true, no tax is applied at all on the order per line item or at the bottom. When I set taxExempt=false, tax is applied per line item and on the total.

All the best,

Daniel

No worries @Daniel_Alsen - glad to hear it’s working now for you!

Just wanted to confirm I’m understanding correctly here - you’d like to have the calculated draft order only show the “full” VAT charges on the full total of the order, but not show up on the separate line items?

There’s not an easy way to do this current, but there may be a way to work around this by creating some logic on your app’s end to first run the calculation with taxes on, and then run the calculation “again” without taxes - we could then add the VAT as a custom line item to the draft order that we actually create (more info here) after it’s calculated based on the taxes we grabbed from the first calculation run.

Just wanted to make sure I’m understanding what you’d like to set up there. If you do need more help with the workaround though, just let me know and I can take a look.