Whenever I try to apply a discount code via the Storefront API, it says the code is not applicable, and it removes all cartLines. Interestingly if I then go and add a product to that very same cartId, the discount code is applied.
If I take that very same discount code, and apply it manually in Shopify Checkout, it works no problem. What could be the issue here? I have verified that the cartId
I’m sending is valid, that the discount works, and the API is not returning any userErrors
Below is a code snippet, showing how I am currently applying the discountCodes (I am using the nodejs app template):
const storefrontClient = new shopify.api.clients.Storefront({ session, apiVersion: '2025-01' });
const mutation = `
mutation cartDiscountCodesUpdate($cartId: ID!, $discountCodes: [String!]) {
cartDiscountCodesUpdate(cartId: $cartId, discountCodes: $discountCodes) {
cart {
lines(first: 10) {
edges {
node {
merchandise {
...on ProductVariant {
product {
title
}
}
}
quantity
}
}
}
discountAllocations {
discountedAmount {
amount
currencyCode
}
targetType
}
discountCodes {
applicable
code
}
}
userErrors {
code
field
message
}
warnings {
code
message
target
}
}
}
`;
const input = {
cartId: "gid://shopify/Cart/Z2NwLWV1cm9wZS13ZXN0MzowMUpNRjhCWlhBQVhZNFNWMUtIQ0tGWUpTSA?key=72d78a..redacted",
discountCodes: ["200000285"]
}
await storefrontClient.query({
data: {
query: mutation,
variables: {...input}
}
});