-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.js
More file actions
48 lines (43 loc) · 1.66 KB
/
checkout.js
File metadata and controls
48 lines (43 loc) · 1.66 KB
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
let listCart = [];
// get data cart from cookie
function checkCart(){
var cookieValue = document.cookie
.split('; ')
.find(row => row.startsWith('listCart='));
if(cookieValue){
listCart = JSON.parse(cookieValue.split('=')[1]);
}
}
checkCart();
addCartToHTML();
function addCartToHTML(){
// clear data default
let listCartHTML = document.querySelector('.returnCart .list');
listCartHTML.innerHTML = '';
let totalQuantityHTML = document.querySelector('.totalQuantity');
let totalPriceHTML = document.querySelector('.totalPrice');
let totalQuantity = 0;
let totalPrice = 0;
// if has product in Cart
if(listCart){
listCart.forEach(product => {
if(product){
let newCart = document.createElement('div');
newCart.classList.add('item');
newCart.innerHTML =
`<img src="${product.image}">
<div class="info">
<div class="name">${product.name}</div>
<div class="price">$${product.price}/1 product</div>
</div>
<div class="quantity">${product.quantity}</div>
<div class="returnPrice">$${product.price * product.quantity}</div>`;
listCartHTML.appendChild(newCart);
totalQuantity = totalQuantity + product.quantity;
totalPrice = totalPrice + (product.price * product.quantity);
}
})
}
totalQuantityHTML.innerText = totalQuantity;
totalPriceHTML.innerText = '$' + totalPrice;
}