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

No invalid parameter error when passing a serialized variant object to addToCart method #147

Closed seanbrookes closed 5 years ago

seanbrookes commented 8 years ago

When I passed a serialized variant object to the addToCart method it returned a cart with a lineItemCount that indicated the variant had been added to the cart but the lineItems was empty. And the checkoutUrl didn't work

JSON parsing the string sorted it out:

cartObj.addVariants({variant: JSON.parse(variant), quantity: qty})

It feels a bit awkward passing the whole variant object to addToCart in the first place.

Would it not be possible to pass a variant.id value instead?

richgilbank commented 8 years ago

@seanbrookes Good question - currently, we need to store the full reference to the variant in the cart, since it's only persisted through localStorage. As a result, all the variant information needs to be available locally, since there's no requests being made to Shopify for additional info. We're currently working on improving the API that backs the SDK to support carts. Once that happens, we'll be able to save just a variant ID, and retrieving the cart would include all necessary information.

Does that help clarify things?

seanbrookes commented 8 years ago

Yes thanks, it makes sense now why I wasn't seeing expected network traffic when interacting with the SDK methods.

Any idea what kind of timeline for enhanced cart support in the SDK?

I still think there should be a sanity check on the variant value passed in addToCart to avoid false positives.