-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.js
79 lines (62 loc) · 2.5 KB
/
checkout.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var cartContainer = document.getElementById("cart-item-container");
//total amount for add the purchases total
var totalAmount = document.getElementById("total-amount");
var numberOfItem = document.getElementById("number-of-item");
var myLocalstorageData = JSON.parse(window.localStorage.getItem("product-list")
);
function createItemOnCheckOut(ipreview, iName, iCount, iPrice) {
var item = document.createElement("div");
item.setAttribute("class", "item");
var itemImg = document.createElement("img");
itemImg.src = ipreview;
var itemDetail = document.createElement("div");
itemDetail.setAttribute("class", "detail");
var itemName = document.createElement("h3");
var itemNameText = document.createTextNode(iName);
itemName.appendChild(itemNameText);
var itemCount = document.createElement("p");
var itemCountText = document.createTextNode("X" + iCount);
itemCount.appendChild(itemCountText);
var itemPrice = document.createElement("p");
var itemPriceText = document.createTextNode("Amount: " + iCount * iPrice);
itemPrice.appendChild(itemPriceText);
itemDetail.appendChild(itemName);
itemDetail.appendChild(itemCount);
itemDetail.appendChild(itemPrice);
item.appendChild(itemImg);
item.appendChild(itemDetail);
return item;
}
for (let i = 0; i < myLocalstorageData.length; i++) {
cartContainer.append(
createItemOnCheckOut(myLocalstorageData[i].preview,
myLocalstorageData[i].title,
myLocalstorageData[i].count,
myLocalstorageData[i].price
)
);
}
var cost = 0;
var counter = 0;
/// calculate the number of item purchases and total amount
for (let j = 0; j < myLocalstorageData.length; j++) {
counter += myLocalstorageData[j].count;
console.log(counter);
cost += parseInt(myLocalstorageData[j].count) *
parseInt(myLocalstorageData[j].price);
console.log(cost);
}
totalAmount.innerHTML = cost;
numberOfItem.innerHTML = counter;
/////Place Order
var placeOrder = document.getElementById("place-order");
placeOrder.addEventListener("click", function() {
var myLocalstorageData = window.localStorage.removeItem("product-list");
cartC = window.localStorage.setItem("cart-count", "0");
var cost = 0;
for (var i = 0; i < myLocalstorageData.length; i++) {
counter += myLocalstorageData[i].count;
}
totalAmount.innerHTML = cost;
numberOfItem.innerHTML = counter;
});