-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.js
52 lines (42 loc) · 1.43 KB
/
cart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function displayCart(){
let cartItems = localStorage.getItem('Cart');
cartItems = JSON.parse(cartItems);
let productContainer = document.querySelector('.products');
let cartCost = localStorage.getItem('totalCost');
if(cartItems && productContainer){
console.log('running')
productContainer.innerHTML = '';
Object.values(cartItems).map(item=>{
productContainer.innerHTML += `
<div class="product-header">
<div class="product-title">${item.name}</div>
<div class="price">${item.price}</div>
<div class="quantity">${item.inCart}</div>
<div class="total">$${item.inCart*item.price}.00</div>
</div>
`
});
productContainer.innerHTML += `
<div class="basketTotalContainer">
<h4 class="basketTotalTitle">
Total
</h4>
<h4 class="basketTotal">
$${cartCost}.00
</h4>
</div>
`
}
}
document.addEventListener("DOMContentLoaded", function () {
displayCart();
var checkoutBtn = document.getElementById('checkoutBtn');
checkoutBtn.addEventListener('click',()=>{
const data = localStorage.getItem('Cart');
const total = localStorage.getItem('totalCost');
firebase.database().ref('orders/' + Math.floor(Math.random()*100)).set({
cart:data,
Total:total
});
})
});