forked from Kashish-Bhargava/ecommerce-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
287 lines (258 loc) · 7.5 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
var cart = [];
function addToCart1() {
var product = {
image: "images/product/f1.jpg",
name: "Colourful Pattern Shirt",
price: 499,
quantity: 1,
subtotal: 499
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart2() {
var product = {
image: "images/product/f2.jpg",
name: "Patterned Resort Shirt",
price: 499,
quantity: 1,
subtotal: 499
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart3() {
var product = {
image: "images/product/f3.jpg",
name: "Vintage Beige Floral Shirt",
price: 499,
quantity: 1,
subtotal: 499
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart4() {
var product = {
image: "images/product/f4.jpg",
name: "Flowers Sleeve Lapel Shirt",
price: 499,
quantity: 1,
subtotal: 499
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart5() {
var product = {
image: "images/product/f5.jpg",
name: "Vintage Floral Oil Shirt",
price: 599,
quantity: 1,
subtotal: 599
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart6() {
var product = {
image: "images/product/f6.jpg",
name: "Vintage Henley Shirt",
price: 799,
quantity: 1,
subtotal: 799
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart7() {
var product = {
image: "images/product/f7.jpg",
name: "Leirke Capri Pants",
price: 399,
quantity: 1,
subtotal: 399
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart8() {
var product = {
image: "images/product/f8.jpg",
name: "Element Pattern Print Top",
price: 699,
quantity: 1,
subtotal: 699
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart9() {
var product = {
image: "images/product/n1.jpg",
name: "Regular Fit Linen-blend grandad shirt",
price: 499,
quantity: 1,
subtotal: 499
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart10() {
var product = {
image: "images/product/n2.jpg",
name: "Grey Striped shirt",
price: 499,
quantity: 1,
subtotal: 499
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart11() {
var product = {
image: "images/product/n3.jpg",
name: "Classic White Shirt",
price: 499,
quantity: 1,
subtotal: 499
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart12() {
var product = {
image: "images/product/n4.jpg",
name: "Bristum 1-Pocket Service Straight Shirt",
price: 399,
quantity: 1,
subtotal: 399
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart13() {
var product = {
image: "images/product/n5.jpg",
name: "Solid Denim Shirt",
price: 599,
quantity: 1,
subtotal: 599
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart14() {
var product = {
image: "images/product/n6.jpg",
name: "Comfort Fit Half-Pant",
price: 499,
quantity: 1,
subtotal: 499
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart15() {
var product = {
image: "images/product/n7.jpg",
name: "Men Beige Solid Slim Fit Shirt",
price: 599,
quantity: 1,
subtotal: 599
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function addToCart16() {
var product = {
image: "images/product/n8.jpg",
name: "Grey Regular Fit Shirt",
price: 399,
quantity: 1,
subtotal: 399
};
cart.push(product);
// updateCart();
// Save the cart in local storage
localStorage.setItem("cart", JSON.stringify(cart));
}
function updateCart() {
var table = document.getElementById("cart");
var subtotal = 0;
// Load the cart from local storage
if (localStorage.getItem("cart")) {
cart = JSON.parse(localStorage.getItem("cart"));
}
table.innerHTML = "<tr><th>Remove</th><th>Image</th><th>Product</th><th>Price</th><th>Quantity</th><th>Subtotal</th></tr>";
for (var i = 0; i < cart.length; i++) {
var product = cart[i];
var row = table.insertRow(-1);
var removeCell = row.insertCell(0);
var imageCell = row.insertCell(1);
var nameCell = row.insertCell(2);
var priceCell = row.insertCell(3);
var quantityCell = row.insertCell(4);
var subtotalCell = row.insertCell(5);
removeCell.innerHTML = "<a href='#' onclick='removeFromCart(" + i + ")'><i class='far fa-times-circle'></i></a>";
imageCell.innerHTML = "<img src='" + product.image + "' alt='" + product.name + "'>";
nameCell.innerHTML = product.name;
priceCell.innerHTML = "Rs. " + product.price;
quantityCell.innerHTML = "<input type='number' name='quantity' min='1' max='10' value='" + product.quantity + "' onchange='updateQuantity(" + i + ", this.value)'>";
subtotalCell.innerHTML = "Rs. " + product.subtotal;
subtotal += product.subtotal;
}
document.getElementById("subtotal").innerHTML = "Rs. " + subtotal;
document.getElementById("total").innerHTML = "Rs. " + subtotal;
}
function updateQuantity(index, quantity) {
var product = cart[index];
product.quantity = quantity;
product.subtotal = product.price * quantity;
updateCart();
}
function removeFromCart(index) {
cart.splice(index, 1);
updateCart();
}
function applyCoupon() {
var coupon = document.getElementById("coupon").value;
if (coupon == "SAVE10") {
var total = parseInt(document.getElementById("total").innerHTML.split(" ")[1]);
total -= 10;
document.getElementById("total").innerHTML = "Rs. " + total;
}
}
window.onload = function () {
updateCart();
};