Shopify / js-buy-sdk

The JS Buy SDK is a lightweight library that allows you to build ecommerce into any website. It is based on Shopify's API and provides the ability to retrieve products and collections from your shop, add products to a cart, and checkout.
https://shopify.github.io/js-buy-sdk
MIT License
989 stars 261 forks source link

Outdated LineItem appears with null variant #615

Closed ttillberg closed 5 years ago

ttillberg commented 5 years ago

Describe the bug I'm using the JS API to display products and create a custom shopping cart. I'm having an issue with the checkout cart and I believe it happens when you add a product to the cart that in its turn gets deleted from stock (from an admin point of view).

What happens is that after fetching the cart again (with the session ID of the cart stored in localstorage), the outdated product still exists within the lineItems array but this time the variant is set to null. Because the variant is no more accessible I can't display the cart item properly with for example a warning message (i.e. thumbnail and price missing from the variant node). What's even more confusing - and this one seems to be hard to get around - is that the Total actually includes the price of the missing product/variant.

Now, if I go to the checkout page, I'm actually getting a warning saying "Variant out of stock / Some items are no longer available. Your cart has been updated." (However I don't think a customer would go to the checkout when the price displaying is wrong)

For now the only way I can figure to solve this would be to iterate the line items at the beginning of the session and check if there's a product with a missing variant. If so, return a new cart instead of the erroneous.

To Reproduce

Note: I'm not allowed to play around with the real products since the shop is live but I do have a cart ID with which I can replicate the error (a product I added to the checkout got deleted after xmas). This is how I proceeded:

Step 1 - Customer:

Step 2 - Admin:

Step 3 - Customer:

I don't think there's any relevant code here apart from the GraphModel containing a LineItem with variant: null

Expected behavior I would expect LineItems corresponding to discarded products would:

Environment (please complete the following information):

Additional context

Bug Report Checklist

rebeccajfriedman commented 5 years ago

I think you have a few options:

ttillberg commented 5 years ago

Thanks for your answer @rebeccajfriedman

I opted for the first option. It's far from the best from a user perspective but it's the safest.
So I'm basically proof-checking all the LineItems against anomalies before eventually passing on the Checkout to the rest of the application.

I somehow feel this procedure should be done at an earlier stage (or ideally the Variant should still be there since the LineItem does exist?). but I also realise this isn't directly related to the SDK but rather the Checkout API. Let's leave it here...

Thanks again for your support and work on this lib !