cartDiscountCodesUpdate removes all cartLines, and does not apply codes

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}
  }
});

Hi Magnus,

From digging into this, it does seem that this is a bug with the cartDiscountCodesUpdate mutation which our team are already aware of and are addressing. I will update here when it’s resolved.

1 Like

Hello,

I was searching on this forum for a similar bug I have on my side:

Every time I create a cart through GraphQL cartCreate and provide a free shipping discount code (manually created or created by an app), the checkout page opens, applies the code for 1 second, and then removes it. As for Magnus, if I add manually the discount code, it is applied.

@Liam-Shopify Should I raise another issue in the forum or are those 2 problems related?

We have many customers impacted on our side, trying to finalize checkout and losing the benefit of the free shipping because somehow Shopify removes it from the cart without clear reasons.

Your help and a quick fix would be greatly appreciated.
Thank you

edit: problem solved in our case. cf The free shipping discount code, added to a cart through cartCreate, is automatically deleted when opening the checkout URL