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: "
}