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

can't get createCart() to work #129

Closed kevinzaki closed 8 years ago

kevinzaki commented 8 years ago

Can't figure this out. (Most of the below code is for test purposes)

Fetching all products works: (The reason I am showing the working code is so you can see everything else is right, the cart route immediately follows the products route.

myRouter.route('/products')
  .get(function(req, res){
    shopClient.fetchQueryProducts({collection_id: 233148358})
      .then(function (product) {
         console.log(product);
      })
      .catch(function () {
        console.log('Request failed');
      })
});

The following to createCart and log the cart ID does not work.

myRouter.route('/cart').get(function(req, res){
  shopClient.createCart()
    .then(function (newCart) {
      console.log(newCart.id);
  })
  .catch(function () {
    console.log('Request failed');
  })
});
minasmart commented 8 years ago

A couple things: Are you planning on managing cart associations to users yourself? If you're not, use fetchRecentCart. Also, if you console.error(error) the error that's sent to catch, can you post the trace?

kevinzaki commented 8 years ago

I do plan on managing cart associations. Basically my plan is to use the buy SDK to generate and manage carts for products stored on Shopify. Once I have a completed cart I am going to use the regular API to create an order and process payments separately. The error I am getting is the following:

[ReferenceError: localStorage is not defined]

minasmart commented 8 years ago

What environment are you running this in? Node or browser?

kevinzaki commented 8 years ago

Node, running my server using Gulp locally at the moment.

minasmart commented 8 years ago

Ah... OK. Yeah. This is a thing we failed to implement.

For the time being, somewhere in your startup code, if you did something like

global.localStorage = {
  _storage: {},
  setItem: function(key, value) {
    this._storage[key] = value;
  },
  getItem: function(key) {
    return this._storage[key]
  },
  clear: function() {
    this._storage = {};
  }
};

I understand this is ugly, but it'll get you by for now. I'll post here when we make this functionality work properly in node.

kevinzaki commented 8 years ago

Thanks it worked, wouldn't of figured that out without some help.

muzuitech commented 5 years ago

How can I set up background colors and other stuffs using createCart() ? Anyone have idea. I am researching but not able to find anything which can solve.

Here is the code :

ui.createCart({ "styles": { "button": { "background-color": "#cccccc", "font-family": "Raleway, sans-serif", "font-size": "18px", "padding-top": "17px", "padding-bottom": "17px", ":hover": { "background-color": "#cccccc" }, ":focus": { "background-color": "#cccccc" }, "font-weight": "normal" }, "footer": { "background-color": "#cccccc" } }, "googleFonts": [ "Raleway" ] });

Thanks

rebeccajfriedman commented 5 years ago

@muzuitech Can you please create a new issue for this in the https://github.com/Shopify/buy-button-js repo?