How do I clear the cart after an order is successful (I am creating using Storefront API)?

How do I clear the cart after an order is successful (I am creating using Storefront API)?

petergAtArrived
Tourist
10 0 2

I want to be able to clear the cart. I use the cart token to create checkout objects which I forward to a custom checkout Flow. However, doing so doesn't seem to generate a new cart token like how Shopify's native checkout would do. It also doesn't clear it out leaving my repeat customers to have items stacked in.

Replies 4 (4)

hassain
Shopify Staff (Retired)
624 104 188

Hey @petergAtArrived ,

 

The Storefront API does not have any reference to the 'Cart'. There are no nodes or queryable objects in this API that correspond to the Cart. Instead a 'Checkout' object is created and associated to customers, and information about the items the customer wishes to order is kept in this Checkout object.

 

As long as the Checkout is being successfully completed, then the next time the customer visits they shouldn't have any of their previous items present still or stacked. For non-completed checkouts, if you want to no longer keep track of it then the other alternative would be to use this mutation to disassociate the customer from this checkout:   https://shopify.dev/docs/storefront-api/reference/mutation/checkoutcustomerdisassociatev2

 

To learn more visit the Shopify Help Center or the Community Blog.

petergAtArrived
Tourist
10 0 2

Hi @hassain . I appreciate your response, thank you for your time.

 

Hm, it's hard to dissociate the checkout from the cart, since it's the only way we're linking what they shop in the storefront and creating a fresh checkout via API on our Checkout app. I create new Checkouts using Shopify's cart token (which contains all their current line_items), so even if I dissociate a customer from a checkout (which I get around by simply creating new checkout objects every time they even land on the page).

 

I'm looking for some solution which could detect whether their checkout or order was tied to their current cart (via Storefront API or another public ajax call), then I can simply reset that cookie/token and problem solved.

 

Best,

Peter

hassain
Shopify Staff (Retired)
624 104 188

Hey @petergAtArrived ,

 

Happy to help out!

 

Just so my understanding is correct, it appears that you are using the regular Shopify online web storefront for thing such as tracking the customer's cart, but then you are also using the Storefront API to generate a checkout? If this is the case, this is definitely a peculiar and not the standard set-up. The Storefront API is meant to be used to build new custom storefronts outside of the regular Shopify online web storefront, not meant to be used on top of it.

 

To the best of my knowledge, there is no property or API that can associate a "Checkout ID/Token" or "Order ID" with a "Cart Token". Your best bet would be to use the Customer as your means of checking this - if the customer's last checkout was successful and became an order, clear their cart on their return. If the customer's last checkout was incomplete, keep their cart as is with all of the previous items stacked in.

 

 

To learn more visit the Shopify Help Center or the Community Blog.

petergAtArrived
Tourist
10 0 2

Hey @hassain thanks again for the quick reply.

 

Yes, you are correct. We are using the standard Shopify online web storefront as our regular e-commerce experience and then direct them to a custom checkout web app that we use the Storefront API for. Hm, I see. I have run into roadblocks for this because of this method. We wanted a simple way to rebuild custom checkout experience (like our own Recharge or CartHook app for example) - I figured that was what they were doing in a much grander SaaS method.

 

Thanks for your input, I think that gave me an idea. I can check to see if I'm able to set a Cookie on our web checkout (which is a subdomain of our normal domain). Then access that same Cookie back on our root domain which can then check whether the cart token has been attached to a successful order.

 

The overkill solution would have to be keeping that data in our internal database and make a quick check against these cart tokens via API. This would not be ideal as we'd have to fire for every customer 😕

 

Best,

Peter