-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
78 lines (53 loc) · 2.06 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
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
var cartitems = JSON.parse(localStorage.getItem("Cart_Items"))
console.log(cartitems)
displayCart(cartitems)
function displayCart(cartitems) {
document.querySelector("tbody").textContent = ""
cartitems.map(function(data, index) {
var tr = document.createElement("tr");
var td1 = document.createElement("td")
var img = document.createElement("img")
img.setAttribute("src", data.imgUrl1)
td1.append(img)
var td2 = document.createElement("td")
td2.textContent = data.name;
var td3 = document.createElement("td")
td3.textContent = "1";
var td4 = document.createElement("td")
td4.textContent = "RS" + " " + data.price + ".00";
var td5 = document.createElement("td")
td5.textContent = "RS" + " " + data.price + ".00"
var td6 = document.createElement("td")
td6.innerHTML = "Delete"
td6.addEventListener("click", function() {
deleteItems(index)
})
tr.append(td1, td2, td3, td4, td5, td6)
document.querySelector("tbody").append(tr);
})
}
// Delete Items here
function deleteItems(index) {
cartitems.splice(index, 1)
localStorage.setItem("CartItems", JSON.stringify(cartitems))
displayCart(cartitems)
}
// show total Price
var total = cartitems.reduce(function(acc, cv) {
return acc + Number(cv.price);
}, 0)
document.querySelector("#subtotal").textContent = `Sub-Total: RS ${total}.00`
document.querySelector("#total").textContent = `Total: RS ${total}.00`
// Apply Coupon here
document.querySelector("form").addEventListener("submit", function(event) {
event.preventDefault()
var coupon_no = document.querySelector("#Coupon").value;
if (coupon_no == "masai30") {
total = Math.floor((30 / 100) * total)
document.querySelector("#subtotal").textContent = `Sub-Total: RS ${total}.00`
document.querySelector("#total").textContent = `Total: RS ${total}.00`
alert("Coupon Applied Successfully")
} else {
alert("Please enter correct coupon no.")
}
})