[GraphQL Storefront API] Get cart id by customer

I couldn’t find any solution to get cart id by customer.
Our application require a customer auth for shopping, so we always have the customer data in hand. So we always create carts with buyerIdentity (customerAccessToken and email)

We would like to show the same cart to our customers on web and in all the connected device app. Without this feature we can not use a shared cart in different platforms.

I have found an old (2023) topic about it, but this solution does not meet with our needs. How to get cart details or cart ID for a given customer in Storefront API - Custom Storefronts - Shopify Community
We would like to achive this with graqhQL Storefront API features.

Thanks, Adam

1 Like

Hi Adam,

At this time there is no way to retrieve a cart from a specific customer using the Storefront API only. The only way to retrieve a cart via the Storefront API is with the cart’s id.

However I do have a couple different workarounds I can suggest:

  1. Most Recommended: When the cart is created on one storefront, you save the cart id and customer id to a backend database on your end that can be accessed by any Headless Storefronts clients to retrieve the associated cart from the customer id.

  2. Using the Customer Account API, make a metafieldsSet mutation call to save the customer’s last cart id in a custom metafield on the Customer Object. Then you can use the Storefront API to retrieve the cart id from the metafield on any other storefront.

  3. Using the Customer Account API, you can access the customer.lastIncompleteCheckout field to get info on the customers last abandoned checkout, including a URL to redirect them back to the checkout. However this does require the customer to have accessed and interacted with the checkout at least once from a cart.

If you were not wanting to handle any back end apps and databases, then workaround 2 would probably be your best bet. Here’s some more info on how you could implement that in more detail.

On Storefront 1:

  • Create Customer Account Token and Cart
  • Uses Customer Account Token with Customer Account API to store the cart id in a metafield on the Customer object with the metafieldsSet mutation.

On Storefront 2.

  • Creates new Customer Account Token (each token is authenticated per storefront)
  • Uses Customer Account Token to retrieve the Customer object from the Storefront API, with a customer query

Here’s some additional documentation on using the Customer Account API with headless storefronts:

Thank you very much for your detailed feedback.

As we currently do not have a backend solution, we are unable to implement the first method you suggested. We are therefore relying exclusively on the Storefront API.

Our team is developing applications for more than seven Shopify stores, and we believe this feature would be highly beneficial within the Storefront API. I noticed that this functionality was requested back in 2023, which is why I am kindly submitting this request again.

Thank you in advance for your consideration.

Hi Adam,

I have submitted some feedback internally on your behalf with this request. While we can’t guarantee when or if changes will be made, I can assure you all feedback is taken with great value by our developers and product managers when determining future features and updates to the platform.

1 Like

hey @adam.matis

did you found any solution for fetching cart id via storefront API?

Thanks.

Hi @Girish_Rajwani,

Unfortunately at this time there is no way to retrieve a cart without the cart id using only the Storefront API, as mentioned in my above posts.

I’m happy to submit some feedback regarding this internally on your behalf as well, the more interest we get on certain feedback topics, the more likely it will be added, though again I can’t guarantee when or if it will be added, just that we do take all feedback into great value when determining future features and updates.

@Kellan-Shopify Developers have been facing this issue since 2023, and I believe it should be available by now. Most importantly, in legacy customer accounts, if someone wants to add products, they can add them via add.js, which doesn’t create a new cart every time, but with a new customer account, a new cart is created. There should be consistency between legacy and new customer accounts.

I want to add a subscription product to the cart and redirect to the cart without impacting the existing cart—this is possible in the legacy customer account but not in the new customer account extension.

I hope it makes sense.

any alternate solution would be really appreciated

Thanks