thetrevorharmon / gatsby-theme-shopify-manager

The easiest way to start a Shopify shop on Gatsby.
https://gatsby-theme-shopify-manager.netlify.app/
MIT License
122 stars 12 forks source link

Remove items from cart if products are removed from sale #90

Open lukebennett88 opened 3 years ago

lukebennett88 commented 3 years ago

If a product is set to draft, archived, or deleted in Shopify — they stay in the cart.

The variant key for a line item (under cart.lineItems) is null, so we are unable to get the variantId in order to use the Shopify Buy SDK to remove that line item from cart.

The best I can come up with is to clear the entire cart if this happens, which isn't ideal. Something like this in the Context Provider:

const cartHasInvalidLineItems = cart.lineItems.find(
  ({ variant }) => variant == null
);

if (cartHasBeenPurchased || cartHasInvalidLineItems) {
  getNewCart();
} else {
  setCart(refreshedCart);
}

Maybe throw up an alert to notify the user why their cart has been emptied?

Jamal1337 commented 3 years ago

I have the same issue. Your code works for now, thanks for that, but it only works after the second refresh?

So when opening the page the initial cart object is wrong as it still states that the variant is available although there is none and it only updates after refreshing the page. Any advice on how to fix that, that it updates on runtime or first page load?