Crinsane / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
3.67k stars 1.73k forks source link

Take away the items #534

Open absemetov opened 5 years ago

absemetov commented 5 years ago

I have been using the basket for a long time, recently when editing an order, I discovered that some items disappear from the basket. I have not found the reason yet. https://rzk.com.ua/c/electric-goods/sockets-switchers/viko?

absemetov commented 5 years ago

Now I create JS module to save cart items into LocalStorage. This works fine and fast.


  var cartArray = JSON.parse(localStorage.getItem("storage:cart")) || [];
  return {
      load: function(json){
        localStorage.setItem("storage:cart", json);
        cartArray = JSON.parse(localStorage.getItem("storage:cart"))
      },
      getItem: function(id){
        return cartArray.filter(function(value) {
            return value.id == id; 
        });
      },
      toJson: function(type) {
        return JSON.stringify(cartArray.map(function(data) {     
           return {id: data.id, qty: data.qty};
        }));
      },
      clear: function() {
        localStorage.removeItem("storage:cart");
        cartArray = [];
      },
      info: function() {
        var countItems = 0;
        var sumQty = 0;
        var sumItems = 0;
        cartArray.forEach(function(item) {
          countItems++;
          sumQty += item.qty;
          sumItems += item.total;
        });
        return [countItems, Math.round(sumItems * 100) / 100, sumQty];
      },
      addItem: function(data) {
        var qty = parseInt(data.qty, 10);
        if(qty) {
          if(this.getItem(data.id).length) {
            this.getItem(data.id)[0].qty = qty;//edit Item
            this.getItem(data.id)[0].total = data.total;
          } else {
            cartArray.push(data);//create Item
          }
        } else {
          //delete Item
          cartArray = cartArray.filter(function(value) {
            return value.id != data.id; 
          });
        }
        //save array to LS
        var serialCart = JSON.stringify(cartArray);
        localStorage.setItem("storage:cart", serialCart);
      }
  }
}());```