Js-buy-sdk: Clearing cart on successful checkout

Created on 21 Jan 2019  Â·  3Comments  Â·  Source: Shopify/js-buy-sdk

Most helpful comment

I had this exact same comment two days ago (when you posted this). What I did, was to re-fetch the cart on page load and on window.focus, (as the checkout link opens a new window, we can fairly safely assume the user will go away and come back). If the cart has an completedAt time, I then create a fresh new cart

All 3 comments

What I've had to do is check if the orderStatusUrl isn't null (thus an order has been created) and if it isn't null then create a new checkout...

var checkoutID = sessionStorage.getItem('thisWebsiteCheckoutID');
client.checkout.fetch(checkoutID).then(function(checkout) {

    if (checkout.orderStatusUrl === null || typeof(checkout.orderStatusUrl) == 'undefined') {

        buildCart(checkout);

    } else {

        client.checkout.create().then(function(checkout) {
            var checkoutID = checkout.id;
            sessionStorage.setItem('thisWebsiteCheckoutID', checkout.id); // Store the ID in sessionStorage
            buildCart(checkout);
        });

    }

    console.log('checkout ↓');
    console.log(checkout);

});

I had this exact same comment two days ago (when you posted this). What I did, was to re-fetch the cart on page load and on window.focus, (as the checkout link opens a new window, we can fairly safely assume the user will go away and come back). If the cart has an completedAt time, I then create a fresh new cart

@isjackwild Yours is perhaps more robust as using orderStatusUrl might become conflicting if you intend to use it further down the line.

Was this page helpful?
0 / 5 - 0 ratings