Closed dmitrybelyakov closed 8 years ago
The error I'm getting here is this:
Uncaught (in promise) TypeError: Cannot read property 'image' of undefined
Which seems to happen at this line in cart model.
Hey @dmitrybelyakov, thanks for letting us know. Looks like it's expected to be under the line_items
key in the attributes passed to createCart
. This should work:
shopClient.createCart({ line_items: [{ id: variantId, quantity: 1 }] }).then(function(cart) { ...
Worth noting though, that this won't automatically fetch the variant image or other details, in case you need those to display a cart. I'll update the docs - please let us know if you come across anything else. Thanks!
Hey @richgilbank !
Yeah, that worked, thank you!. But it still behaves weirdly. Can you please tell me what's the purpose of this functionality? Because:
Hmm, interesting - this should be able to function (to a limited extent) with just the variant IDs. Ideally, you'd be able to at the very least transition to checkout, so that's something worth investigating. As for fetching every product in the cart, there's a few things worth noting:
shopClient.fetchQueryProducts(product_ids: [1, 2, 3])
- this is not yet well documented, but being discussed in https://github.com/Shopify/js-buy-sdk/issues/67Hope this helps!
Thank you, @richgilbank . For now we managed to work around this issue by putting fetched products to localStorage and then initialising cart from that.
Great, thanks! The SDK's still pretty new and rough around the edges so feedback is really helpful. 😄
@richgilbank haha yeah, I figured it's only 3 months old. But still very useful! Thanks a lot for your help!
@richgilbank, I wonder why this has been closed. I'm getting the same thing:
When cart is initialised that way, checkout url we get looks like this: https://shop.myshopify.com/cart/undefined:1?api_key=123. That obviously won't work.
When i replace undefined
with the variant id i provided, the checkout url works. ¯(ツ)/¯
Hey there! The whole cart system is currently in flux, and initializing with line item attrs is pretty complex.
The best way way to do this is something like
const cart = client.createCart();
cart.addVariants(product.selectedVariant);
The reason for this is that with a locally persisted cart, and only variant ids, there's no way for us (with our existing APIs) to track that variant back to its product and grab images and things like that.
We are currently working on a better way, and we have updated docs in #233 that should be shipping shortly that will better reflect this method.
Hi @minasmart! Ahh i see, thank you for the workaround.
Hi, I'm following official documentation from Javascript Buy SDK Guide - initialising cart with variants in fact does nothing for me, apart from creating an empty cart:
using: shopify-buy.polyfilled.globals.min.js