let carts = document.querySelectorAll('.btn'); let products = [ { name: "Camouflage Crossbody Bag", tag: "btn 1", tag:"PhotoRoom_013_20220219_091616", price: 30.00, inCart: 0 }, { name: "Storm Trooper Crossbody Bag", tag: "btn 2", tag:"PhotoRoom_016_20220219_091616", price: 30.00, inCart: 0 }, { name: "Gamer Crossbody Bag", tag: "btn 3", tag:"PhotoRoom_015_20220219_091616", price: 30.00, inCart: 0 }, { name: "Care Bear Scrunchie Set 2pcs", tag: "btn 4", tag:"PhotoRoom_011_20220219_091616", price: 10.00, inCart: 0 }, { name: "Gamer Scrunchie Set 2pcs", tag: "btn 5", tag:"PhotoRoom_005_20220219_091616", price: 10.00, inCart: 0 }, { name: "Mustache Scrunchie Set 2pcs", tag: "btn 6", tag:"PhotoRoom_006_20220219_091616", price: 10.00, inCart: 0 }, { name: "Black & White Scrunchie Set 2pcs", tag: "btn 7", tag:"PhotoRoom_009_20220219_091616", price: 10.00, inCart: 0 }, { name: "Pastel Rainbow Scrunchie Set 2pcs", tag: "btn 8", tag:"PhotoRoom_001_20220219_091616", price: 10.00, inCart: 0 }, { name: "Floral Scrunchie Set 2pcs", tag: "btn 9", tag:"PhotoRoom_20220219_091914", price: 10.00, inCart: 0 }, ]; for (let i=0; i < carts.length; i++) { carts[i].addEventListener('click', () => { cartNumbers(products[i]); totalCost(products[i]) }) } function onLoadCartNumbers() { let productNumbers = localStorage.getItem('cartNumbers'); if(productNumbers) { document.querySelector('.cart span').textContent = productNumbers; } } function cartNumbers(product, action) { let productNumbers = localStorage.getItem('cartNumbers'); productNumbers = parseInt(productNumbers); let cartItems = localStorage.getItem('productsInCart'); cartItems = JSON.parse(cartItems); if( action == "decrease") { localStorage.setItem('cartNumbers', productNumbers - 1); document.querySelector('.cart span').textContent = productNumbers - 1; } else if( productNumbers ) { localStorage.setItem("cartNumbers", productNumbers + 1 ); document.querySelector('.cart span').textContent = productNumbers + 1; } else { localStorage.setItem('cartNumbers', 1); document.querySelector('.cart span').textContent = 1; } setItems(product); } function setItems(product) { let cartItems = localStorage.getItem('productsInCart'); cartItems = JSON.parse(cartItems); if(cartItems != null) { if(cartItems[product.tag] == undefined) { cartItems = { ...cartItems, [product.tag]: product } } cartItems[product.tag].inCart += 1; } else { product.inCart = 1; cartItems = { [product.tag]: product } } localStorage.setItem("productsInCart", JSON.stringify(cartItems)); } function totalCost(product, action) { let cartCost = localStorage.getItem('totalCost'); console.log("My cartCost is", cartCost); console.log(typeof cartCost ); if( action == "decrease") { cartCost = parseInt(cartCost); localStorage.setItem('totalCost',cartCost - product.price); } else if(cartCost != null) { cartCost = parseInt(cartCost); localStorage.setItem("totalCost", cartCost + product.price); } else { localStorage.setItem("totalCost", product.price); } } function displayCart() { let cartItems = localStorage.getItem("productsInCart"); cartItems = JSON.parse(cartItems); let productContainer = document.querySelector(".products"); let cartCost = localStorage.getItem('totalCost'); //console.log(cartItems); if( cartItems && productContainer ) { productContainer.innerHTML = ''; Object.values(cartItems).map( (item => { productContainer.innerHTML += `