-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcart.html
174 lines (143 loc) · 5.22 KB
/
cart.html
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#cart_items {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
margin-left: 2%;
margin-bottom: 5%;
margin-top: 2%;
}
#cart_items img {
width: 422px;
}
#cart_items>div {
border-radius: 20px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
#cart_items>div p {
padding-left: 5%;
}
#checkout_btn {
margin-left: 36%;
padding: 1%;
border-radius: 25px;
color: white;
background-color: red;
font-size: 20px;
font-weight: bolder;
margin-bottom: 2%;
}
button {
margin-left: 24%;
padding: 1%;
border-radius: 25px;
color: white;
background-color: red;
font-size: 20px;
font-weight: bolder;
margin-bottom: 2%;
cursor: pointer;
}
h1 {
width: 100vw;
text-align: center;
margin-top: 20vh;
}
</style>
<title>Cart Page</title>
</head>
<body>
<div id="cart_items"></div>
<button id="checkout_btn" onclick="paymentPage()">Checkout and Proceed to Payment Page</button>
<script>
let cart_dom = document.getElementById(`cart_items`)
if(JSON.parse(localStorage.getItem(`add_to_cart`)).length == 0){
let h1 = document.createElement(`h1`)
h1.innerHTML = `<a href="menu.html">Cart is Empty Go back to Menu Page<a>`
cart_dom.append(h1)
document.getElementById(`checkout_btn`).style.display = "none"
}
let cart_data = JSON.parse(localStorage.getItem(`add_to_cart`))
cart_data.forEach((item) => {
let div = document.createElement(`div`)
let img = document.createElement(`img`)
img.src = item.image
let h3 = document.createElement(`p`)
h3.innerHTML = `<b>${item.name}<b>`
h3.style.fontSize = "18px"
let p1 = document.createElement(`p`)
p1.innerHTML = "₹ " + `<b>${item.price}</b>`
let div_2 = document.createElement(`div`)
div_2.className = "description"
let p2 = document.createElement(`p`)
p2.textContent = item.description
p2.style.fontSize = "15px"
p2.style.color = "gray"
div_2.append(p2)
let rmv_btn = document.createElement(`button`)
rmv_btn.textContent = "Remove Item from Cart"
rmv_btn.addEventListener(`click`, () => {
domUpdate(item)
})
div.append(img, h3, p1, div_2, rmv_btn)
cart_dom.append(div)
})
const paymentPage = () => {
location.href = "./payment.html"
}
const domUpdate = (item) => {
let new_arr = []
let data = JSON.parse(localStorage.getItem(`add_to_cart`))
data.forEach((ele) => {
if(ele.name != item.name){
new_arr.push(ele)
}
})
localStorage.setItem(`add_to_cart`, JSON.stringify(new_arr))
displayData(new_arr)
}
const displayData = (new_arr) => {
if(JSON.parse(localStorage.getItem(`add_to_cart`)).length == 0){
cart_dom.textContent = ""
let h1 = document.createElement(`h1`)
h1.innerHTML = `<a href="menu.html">Cart is Empty Go back to Menu Page<a>`
cart_dom.append(h1)
document.getElementById(`checkout_btn`).style.display = "none"
}
else{
cart_dom.textContent = ""
new_arr.forEach((item) => {
let div = document.createElement(`div`)
let img = document.createElement(`img`)
img.src = item.image
let h3 = document.createElement(`p`)
h3.innerHTML = `<b>${item.name}<b>`
h3.style.fontSize = "18px"
let p1 = document.createElement(`p`)
p1.innerHTML = "₹ " + `<b>${item.price}</b>`
let div_2 = document.createElement(`div`)
div_2.className = "description"
let p2 = document.createElement(`p`)
p2.textContent = item.description
p2.style.fontSize = "15px"
p2.style.color = "gray"
div_2.append(p2)
let rmv_btn = document.createElement(`button`)
rmv_btn.textContent = "Remove Item from Cart"
rmv_btn.addEventListener(`click`, () => {
domUpdate(item)
})
div.append(img, h3, p1, div_2, rmv_btn)
cart_dom.append(div)
})
}
}
</script>
</body>
</html>