-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcart-page.js
99 lines (71 loc) · 2.71 KB
/
cart-page.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
let cartArr = JSON.parse(localStorage.getItem("Pcart")) || []
if (cartArr.length == 0) {
document.querySelector("body").innerHTML = "<p>Oops!<br>Looks like there is no item in your cart yet.</p > "
alert("Your Cart Is Empty")
window.location.href="product-page.html"
} else {
let Pquantity = cartArr.length
//total amount and discount
let subsum = 0;
let dis = 0;
//coupon apply
let coupon = document.querySelector("#promo");
function checkDiscount() {
if (coupon.value == "masai30") {
console.log(coupon.value)
let discount = (+subsum) * 30 / 100
dis = (+subsum) - discount;
alert("Congrats... You got 30% Discount")
document.querySelector("#total").innerText = dis;
document.querySelector("#discount").innerText = 30 + "%"
}
else {
alert("Invalid Promocode")
}
}
displayTable(cartArr)
function displayTable(cartArr) {
let tBody = document.querySelector("tbody");
cartArr.forEach(function (el, index) {
let tr = document.createElement("tr");
let td1 = document.createElement("td");
let pic = document.createElement("img")
pic.src = el.pic
td1.append(pic)
let td2 = document.createElement("td");
td2.innerText = el.pname
let td3 = document.createElement("td");
td3.innerText = "₹ " + el.price;
let td4 = document.createElement("td");
let span2 = document.createElement("span")
let quantity = 1
span2.innerText = quantity
let span4 = document.createElement("span")
span4.innerText = "x"
span4.addEventListener("click", function () {
removeItem(el, index);
});
td4.append(span2, span4)
tr.append(td1, td2, td3, td4)
tBody.append(tr)
subsum = subsum + (+el.price)
let a = document.querySelector("#quantity")
a.innerText = Pquantity
let b = document.querySelector("#subtotal")
b.innerText = "₹ " + subsum
let c = document.querySelector("#discount")
c.innerText = dis + "%"
let d = document.querySelector("#total")
d.innerText = "₹ " + subsum
})
}
function removeItem(elem, index) {
cartArr.splice(index, 1);
localStorage.setItem("Pcart", JSON.stringify(cartArr));
window.location.reload();
}
document.querySelector("#checkout").addEventListener("click",checkout)
function checkout(){
window.location.href="address.html"
}
}