Answer the question
In order to leave comments, you need to log in
How to correctly enter data into the basket using localstorage (json)?
Hello, I’m making a cart on the site using the jQcart plugin, I ran into the problem of adding a product via localstorage and json from a modal window. The essence of the adding algorithm is this, there are product cards on the page, when you click on the button, a modal window appears in which the date attributes are transferred from the product card , after clicking on the button in the modal window and selecting additional parameters, the product goes to the cart. With the first product, the entry works correctly. When entering another product card, the data is added to the previous product, and is not indicated in the basket as a unique product. When checking through google dev tools, as I understand it, the object is not updated, although the attributes in the button have changed and it is simply added to the previous product. And if you add a product without a modal window,
var itemDataButton, totalValueOrder,
itemDataPopUp = $('.delivery-type__addorder_btn');
$('.add-to-cart').click(function (event) {
event.preventDefault();
totalValue = $('#total_value').text($(this).data('price'));
totalValueOrder = ($(this).data('price'));
itemDataButton = $(this).data();
for ( var key in itemDataButton) {
itemDataPopUp.attr('data-' + key, itemDataButton[key]); } });
addToCart: function(e) {
e.preventDefault();
itemData = $(this).data();
if(typeof itemData.id === 'undefined') {
console.log('Отсутствует ID товара');
return false;
}
cartData = actions.getStorage() || {};
if (cartData.hasOwnProperty(itemData.id)) {
cartData[itemData.id].count++;
} else {
itemData.count = 1;
cartData[itemData.id] = itemData;
}
actions.setStorage(cartData);
actions.changeTotalCnt(1);
label.show();
if(opts.openByAdding) {
actions.openCart();
}
return false;
},
setStorage: function(o) {
localStorage.setItem('jqcart', JSON.stringify(o));
return false;
},
getStorage: function() {
return JSON.parse(localStorage.getItem('jqcart'));
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question