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:
-
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.
-
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.
-
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
Hi @Girish_Rajwani,
What you described here is not actually a difference between Legacy Customer Accounts, and New Customer Accounts, but is actually regarding the use of the Online Store - Liquid Theme Storefronts and Headless Storefronts.
The Online Store Channel’s Liquid Storefronts, use the AJAX API (with add.js) to add products to carts that are stored as cookies as a part of the Liquid Storefront, that’s why it doesn’t create a cart every time, since it uses the cart existing in the customers web browser cookies.
Where as with Headless Storefronts using the Storefront API, it is expected that the developer implements their own storage and management of the customers carts, since the point of a Headless Storefront using the API is that the full website is hosted and developed externally, and interfaces with Shopify using the Storefront and Customer Account APIs. This means that you as the storefront developer would create the cart with the Storefront API, and save the cart details in an external database, or using cookies in your storefront implementation. This why there is no current way to retrieve carts with the Cart ID on the Storefront API, as it is expected that you are managing the cart storage externally, and will have the cart id available when needed.
If you’re using the Hydrogen and Oxygen stack to develop the Headless Storefront, this is implemented for you within the Hydrogen framework. Here’s some documentation with more info:
General Hydrogen Docs:
Handling Cart, including getting the Cart ID from the session cookie:
If you are not using the Hydrogen and Oxygen stack, you can develop a Hydrogen Storefront and host it yourself, using our Hydrogen React library, or you can impelement your own storefront that uses our Storefront API, though you will have to handle cart storage yourself as mentioned. Here’s some more documentation and examples for this as well:
Hey @Kellan-Shopify thanks for detailed info.
The point is i am not using headless, i am using online store theme with new customer account and i need to update cart in the customer account extension but I can’t update as compared to cart/add.js in legacy account. That’s what i meant by the difference between new and legacy. There should be uniformity
Thanks.
Hi @Girish_Rajwani,
Can you share with me some more context on exactly what you are trying to do, including:
- any documentation you are following
- any API calls you are making (including full examples of the calls)
- detailed explanation of what you expected to happen vs what is actually happening?
I’m not quite sure I fully understand what you are trying to do and how you are trying to do it. And if you can provide me with more details perhaps I can offer more specific guidance or workarounds applicable for your usecase.
Hi @Kellan-Shopify
I have built an theme embed extension for legacy account and customer account ui extension for new customer account.
In embed extension on legacy account page, i can use cart/add.js and it will update the cart instead of creating new cart - Which is perfect and working as expected
However, on new customer account page, i am using customer account extension and in that cart/add.js doesn’t works and when i use cartcreate mutation, it just creates the new cart, instead it should update it - that’s the issue.
Hope i am able to explain properly. Let me know if any questions. Thanks.
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:
-
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
-
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.
- this is the same with other Storefront API mutations that edit carts, such as:
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.
hi @Kellan-Shopify
thank you that makes sense. one last question: would it be possible to fetch cart id in the customer account extentsion itself by any chance either by any api call or anything?
Thanks.
Hi @Girish_Rajwani,
Unfortunately there is no way to fetch a cart id in the Customer Account Extension. The only APIs this extension has access to is the Storefront API and the Customer Account API, and neither are able to fetch a cart without having the cart id already.
Additionally the extension has no direct access to the cookies in the browser, as the extension is ran in a separate sandboxed frame, with no access to the browser DOM, so it can’t get the cart id from the browser cookie either.
Hi @Kellan-Shopify
I hope developer team is aware of it and they will introduce a fix for it.
Thanks.
Hi @Girish_Rajwani,
I can confirm that this is currently expected behaviour as designed, so no immediate fixes will be introduced regarding this.
However, I will absolutely be submitting some feedback internally on your behalf, that you’d like to see a way to fetch carts via the Storefront API, Customer Account API, or the Customer Account UI Extension, without needing an existing cart id.
While we can’t guarantee when or if this will be added, I can assure you that our developers and product managers do take all of our Partner and Merchant feedback into great value when determining future features and platform updates. The more feedback we get for topics like these, the more likely and sooner they would be added.