Open absemetov opened 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);
}
}
}());```
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?