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

Unable to initialise cart with variants. Documentation example not working. #69

Closed dmitrybelyakov closed 8 years ago

dmitrybelyakov commented 8 years ago

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:

//these are actual products
var productId = 5863604999;
var variantId = 18525309447;

//code from the docs: http://shopify.github.io/js-buy-sdk/guide/
shopClient.createCart({id: variantId, quantity: 1}).then(function (cart) {
    console.log('CREATED CART:', cart.lineItems.length); //0
});

//doing it like this doesn't help either
shopClient.fetchProduct(productId).then(function (product) {
    shopClient.createCart({id: product.selectedVariant.id, quantity: 1}).then(function (cart) {
        console.log('CREATED CART:', cart.lineItems.length); //0
    });
 });

using: shopify-buy.polyfilled.globals.min.js

dmitrybelyakov commented 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.

richgilbank commented 8 years ago

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!

dmitrybelyakov commented 8 years ago

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:

richgilbank commented 8 years ago

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:

Hope this helps!

dmitrybelyakov commented 8 years ago

Thank you, @richgilbank . For now we managed to work around this issue by putting fetched products to localStorage and then initialising cart from that.

richgilbank commented 8 years ago

Great, thanks! The SDK's still pretty new and rough around the edges so feedback is really helpful. 😄

dmitrybelyakov commented 8 years ago

@richgilbank haha yeah, I figured it's only 3 months old. But still very useful! Thanks a lot for your help!

yashafromrussia commented 7 years ago

@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. ¯(ツ)/¯

minasmart commented 7 years ago

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.

yashafromrussia commented 7 years ago

Hi @minasmart! Ahh i see, thank you for the workaround.