[GraphQL Storefront API] Get cart id by customer

Hi @Girish_Rajwani,

That definitely helps me understand what you’re trying to do here, and why this is occurring.

This is actually expected behaviour since you are using the Storefront API in the Customer Account Extension, specifically the Storefront API cartCreate mutation will always create a new cart no matter what.

The only way to have the Storefront API edit a cart created from the Online Store, is to have the cart id already, and pass it in mutations such as cartLinesAdd.

For example, your workflow to accomplish would look like so:

  1. On the Online Store pages (not the Customer Account profiles) get the cart id from the customer’s cart in their browser cookies with an AJAX API call

    • Cart API reference - GET /{locale}/cart.js

    • this returns a token field in the JSON response, this full token, including the key parameter, is the cart’s id

    • an alternative to get the Cart ID is to subscribe to the carts/create webhook to get notified of a newly created cart and it’s id

  2. With the cart id returned from the AJAX call, in the Customer Account Extension, you can make the cartLinesAdd mutation, passing the full cart id, including the key parameter, and add new products to the cart.

I tested this on my own store, and I can confirm that the Storefront API does work with cart id’s retrieved from the AJAX API call on the Online Store Storefront.