1
Good morning, everyone. I have a stroller where I record items for an estimate. Until then I can record this item in the cart in the localStorage and persist this data in this same item, the user can change page, close the browser and etc..
But when I add a second item to the cart I cannot keep each selection of each item, only the first.
Code js:
var saveItemCart = function(){
var cart = {
cores: document.getElementById("cor_add").value,
qtd_gomos: document.getElementById("qtd_gomos").value,
qtd_cores: document.getElementById("qtd_cores").value,
quantidade: document.getElementById("quantidade").value,
obs: document.getElementById("obs").value,
};
localStorage.setItem('cart', JSON.stringify(cart));
}
document.onchange = saveItemCart;
if(document.getElementById('id{{$row->id}}')){
if (localStorage.cart) {
document.getElementById('cor_add').value = JSON.parse(localStorage.getItem('cart')).cores;
}
if (localStorage.cart) {
document.getElementById('qtd_gomos').value = JSON.parse(localStorage.getItem('cart')).qtd_gomos;
}
if (localStorage.cart) {
document.getElementById('qtd_cores').value = JSON.parse(localStorage.getItem('cart')).qtd_cores;
}
if (localStorage.cart) {
document.getElementById('quantidade').value = JSON.parse(localStorage.getItem('cart')).quantidade;
}
if (localStorage.cart) {
document.getElementById('obs').value = JSON.parse(localStorage.getItem('cart')).obs;
}}else( localStorage.clear());
How could you define for each item to have its values? If you need more information I edit the post.
Thank you.
Each entry in the Storage location must have a different name, if you call all of Cart will be overwritten, modify the value names of the Storage location and add as many objects as you want there.
– Sabrina
Hi @Sabrinab. thanks for the reply. Overall JS would be right? I understood what you meant, I will try to put the ID that comes from each product.
– adrib
I found no problems beyond the envelope I mentioned.
– Sabrina