We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Re: Using cart token from AJAX api in Storefront API

Using cart token from AJAX api in Storefront API

AncleMarvel
Shopify Partner
4 0 6

Hi there! I have an app which allows customers to apply discount codes on the cart page using the mutation "cartDiscountCodesUpdate" provided by Storefront API. This mutation needs cart id with the following template:

gid://shopify/Cart/<cart.token> where <cart.token> - I receive from a client by fetching the cart using AJAX api because there's no other ways to do this. It works for development stores and for normal stores migrated to new checkout api or recently created stores already with new checkout api. It doesn't work for old stores which are not migrated to the new checkout.

It doesn't work because cartDiscountCodesUpdate mutation can't find the card by id, it returns:

 

 

 

{
message: "The specified cart does not exist.",
errorCode: "INVALID"
}

 

 

 

And you know what? There's no even such issue in the documentation of this mutation: https://shopify.dev/docs/api/storefront/2024-04/mutations/cartdiscountcodesupdate

There is INVALID code but the message is different: "The input value is invalid."

And I also found that the cart token differs on stores with the new checkout and the old one:
Z2NwLWV1cm9wZS13ZXN0MTowMUhaRjZTRlg4RzlTOEZWWUdGUUNaWTA1UA  - token format on the new checkout stores
01313bbad43e0354bd635585beb0dd1c - token format on the old checkout stores

Is there anything which can help me to apply discount codes using graphql mutation? Or maybe I can recreate an id of a cart? So any ideas please! 🙏

Replies 2 (2)

AdrianExpert
Shopify Partner
29 1 2

Any luck with the mutation? I have the same issue with some stores. 

For you to achieve your goals, visitors must first achieve theirs

quique12
Shopify Partner
2 0 0

We're having the same issue!

 

Sometimes the AJAX API (GET /cart.js)

returns tokens in the longer format (Z2NwLXVzLWVhc3QxOjAxSkpOSEhDR05IM0tENlRBV0FDMktUWlBD?key=6184a46df0a4e29cbea6cf658ae903a6)
sometimes in the shorter format (6a6d0880ec1fce20e61deaf9c6f0b188)

 

With the shorter tokens, when we use the `cartDiscountCodesUpdate` mutation to apply a discount to the cart via storefront API, we get a 'cart not found' error. But it works with the longer tokens.

 

Can someone from Shopify please help? This inconsistency in token format is breaking a key feature in our app.