-
Notifications
You must be signed in to change notification settings - Fork 1
/
cart.js
470 lines (408 loc) · 18.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
var container = document.getElementById('container');
cartData = JSON.parse(localStorage.getItem("cartData")) || [];
var extraProd = [
{
proid:1,
name:"A Clav 625 mg Tablet",
location:"Capital Pharma",
price:195.00,
off:20,
cou:0,
catg:"Weight Management",
instock:9,
avail:"Instock",
imgs:"https://th.bing.com/th/id/OIP.59U8Ip0sA2No8W6qp-C-1AHaHa?pid=ImgDet&w=511&h=512&rs=1",
Packing: "6 Tablets / Strip",
pruductdetailes:{
SKU: 'PC-1211',
ProductDescription: 'A Clav 625 mg Tablet is manufactured by Capital Pharma',
Packing: 'Strip',
Condition: 'New',
Soldby: 'Pulse Pharmacy India Pvt. Ltd.'
}
},
{
proid:2,
name:"A Daxid L Tablet",
location:"A Daxid L Tablet",
price:50.00,
off:5.00,
cou:0,
catg:"Beauty",
instock:3,
avail:"Out of Stock",
imgs:"https://th.bing.com/th/id/OIP.59U8Ip0sA2No8W6qp-C-1AHaHa?pid=ImgDet&w=511&h=512&rs=1",
Packing: "10 Tablets / Strip",
pruductdetailes:{
SKU: 'PC-44563',
ProductDescription: 'A Daxid L Tablet is manufactured by Eskon Pharma',
Packing: 'Strip',
Condition: 'New',
Soldby: 'Pulse Pharmacy India Pvt. Ltd.'
}
},
{
proid:3,
name:"Aaranac P Tablet",
location:"Aaranac P Tablet",
imgs:"https://th.bing.com/th/id/OIP.59U8Ip0sA2No8W6qp-C-1AHaHa?pid=ImgDet&w=511&h=512&rs=1",
price:37.50,
off:5.00,
cou:0,
catg:"Beauty",
instock:4,
avail:"In Stock",
Packing: "10 Tablets / Strip",
pruductdetailes:{
SKU: 'PC-2065',
ProductDescription: 'Aaranac P Tablet is manufactured by Aara Life Sciences (I) Pvt Ltd',
Packing: 'Strip',
Condition: 'New',
Soldby: 'Pulse Pharmacy India Pvt. Ltd.'
}
},
{
proid:4,
name:"Gabagesic Gel 30 gm",
location:"Crescent Therapeutics Ltd",
price:232.80,
off:0,
cou:0,
catg:"Medical Device",
instock:3,
avail:"In Stock",
imgs:"https://media.istockphoto.com/vectors/pill-bottle-with-pills-vector-id459969585?k=6&m=459969585&s=612x612&w=0&h=ZyDyz05L-WMSSCs8v8RBxw4mZ-byXDiy008yAyNAv20=",
Packing: "Tube",
pruductdetailes:{
SKU: 'PC-15264',
ProductDescription: 'Gabagesic Gel 30 gm is manufactured by Linux Laboratories',
Packing: 'Tube',
Condition: 'New',
Soldby: 'Pulse Pharmacy India Pvt. Ltd.'
}
},
{
proid:5,
name:"Gabarich 150 mg Tablet",
location:"Crescent Therapeutics Ltd",
price:98.62,
off:0,
cou:0,
catg:"Ayurveda",
instock:1,
avail:"In Stock",
imgs:"https://th.bing.com/th/id/OIP.59U8Ip0sA2No8W6qp-C-1AHaHa?pid=ImgDet&w=511&h=512&rs=1",
Packing: "10 Tablets / Strip",
pruductdetailes:{
SKU: 'SKU: PC-14550',
ProductDescription: 'Gabarich 150 mg Tablet is manufactured by Crescent Therapeutics Ltd',
Packing: 'Strip',
Condition: 'New',
Soldby: 'Pulse Pharmacy India Pvt. Ltd.'
}
},
{
proid:6,
name:"Macbery PD Syrup",
location:"Macleods Pharmaceuticals Pvt Ltd",
price:62.40,
off:5.00,
cou:0,
catg:"Ayurveda",
instock:10,
avail:"In Stock",
imgs:"https://media.istockphoto.com/vectors/pill-bottle-with-pills-vector-id459969585?k=6&m=459969585&s=612x612&w=0&h=ZyDyz05L-WMSSCs8v8RBxw4mZ-byXDiy008yAyNAv20=",
Packing: "Macbery PD Syrup is manufactured by Macleods Pharmaceuticals Pvt Ltd",
pruductdetailes:{
SKU: 'PC-53321',
ProductDescription: 'Macbery PD Syrup is manufactured by Macleods Pharmaceuticals Pvt Ltd',
Packing: 'Macbery PD Syrup is manufactured by Macleods Pharmaceuticals Pvt Ltd',
Condition: 'New',
Soldby: 'Pulse Pharmacy India Pvt. Ltd.'
}
}
];
showCartData(cartData, extraProd);
function showCartData(cartData, extraProd){
container.innerHTML = "";
if(cartData.length===0){
//if elements is not present in cartData then do this.
var bgImage = document.createElement("img");
var bgImageDiv = document.createElement("div");
var btn1 = document.createElement("button");
var btn2 = document.createElement("button");
//setting bgImageDiv
bgImage.setAttribute("src", "https://www.pulseplus.in/content/images/icons/cart-empty.png");
bgImageDiv.setAttribute("id","bgImageDiv");
btn1.textContent = "ADD MEDICINES";
btn1.addEventListener("click",function(){
window.location.href = "ProductPage.html"
});
btn2.textContent = "MY ORDERS";
btn2.addEventListener("click",function(){
window.location.href = "order.html"
})
bgImageDiv.append(bgImage, btn1, btn2);
container.append(bgImageDiv);
}else{
//If elements are present in cart then do this
//creating html elements and then declaring variables.
//creating heading for page.
var pageHeading = document.createElement("h2");
//creating main div to show data.
var mainDiv = document.createElement("div");
//creating left div.
var leftDiv = document.createElement("div");
//creating elements of leftDiv.
var cartProducts = document.createElement("div");
var addMedicineBtn = document.createElement("button");
var productsSlider = document.createElement("div");
//creating right div.
var rightDiv = document.createElement("div");
//creating elements of rightDiv.
var freeOrderBtn = document.createElement("button");
var applyCouponBtn = document.createElement("button");
var orderSummaryDiv = document.createElement("div");
var proceedBtn = document.createElement("button");
//setting values to the above declare variables.
pageHeading.innerHTML = "Cart";
//showing all the products present in the cart in leftDiv part.
var totalSum = 0;
cartData.map(function(element, index) {
//calculating totalSum
totalSum += +element.price*+element.quantity;
var row = document.createElement("div");
var firstDiv = document.createElement("div");
var prodImg = document.createElement("img");
var secondDiv = document.createElement("div");
var title = document.createElement("p");
var divUnderSecondDiv = document.createElement("div");
var price = document.createElement("p");
var deleteBtn = document.createElement("button");
var thirdDiv = document.createElement("div");
var divUnderThirdDiv = document.createElement("div");
var increaseQty = document.createElement("div");
var quantity = document.createElement("p");
var decreaseQty = document.createElement("div")
var finalPrice = document.createElement("p");
//setting up first div
prodImg.setAttribute("src", element.imgs);
firstDiv.appendChild(prodImg);
firstDiv.setAttribute("class", "firstDiv");
//setting up second div
title.textContent = element.name;
divUnderSecondDiv.setAttribute("class", "priceAndButtonDiv");
price.textContent = "₹ " + element.price;
deleteBtn.innerHTML = '<i class="far fa-trash-alt"></i>';
deleteBtn.setAttribute("class", "deleteBtn");
deleteBtn.addEventListener("click", function(){
deleteFromCart(index);
});
divUnderSecondDiv.append(price, deleteBtn);
secondDiv.append(title, divUnderSecondDiv);
secondDiv.setAttribute("class","secondDiv");
//setting up third div
increaseQty.textContent = "+";
increaseQty.setAttribute("class", "increaseQty");
quantity.textContent = element.quantity;
decreaseQty.textContent = "-";
decreaseQty.setAttribute("class","decreaseQty");
increaseQty.addEventListener("click", function(){
increaseQuantity(index);
});
decreaseQty.addEventListener("click", function(){
decreaseQuantity(index);
});
divUnderThirdDiv.setAttribute("class", "divUnderThirdDiv");
divUnderThirdDiv.append(decreaseQty, quantity, increaseQty);
finalPrice.textContent = "₹ " + element.price*element.quantity + " only";
thirdDiv.append(divUnderThirdDiv, finalPrice);
thirdDiv.setAttribute("class","thirdDiv");
//appending Elements.
row.setAttribute("class", "cartRow");
row.append(firstDiv, secondDiv, thirdDiv);
cartProducts.append(row);
});
//setting up classes & Ids to the elements to provide css.
cartProducts.setAttribute("class", "cartProducts")
leftDiv.setAttribute("class", "leftDiv");
rightDiv.setAttribute("class", "rightDiv");
mainDiv.setAttribute("id","mainDiv");
//add Medicines button,
addMedicineBtn.textContent = "+ Add Medicines";
addMedicineBtn.addEventListener("click", function(){
window.location.href = "ProductPage.html"
});
addMedicineBtn.setAttribute("id", "addMedicineBtn")
//add more product to cart wala section.
extraProd.map(function(element, index){
var box = document.createElement("div");
var inBoxDiv1 = document.createElement("div");
var prodImage = document.createElement("img");
var inBoxDiv2 = document.createElement("div");
var prodTitle = document.createElement("p");
var prodPrice = document.createElement("p");
var addBtn = document.createElement("button");
prodImage.setAttribute("src", element.imgs);
inBoxDiv1.appendChild(prodImage);
prodTitle.textContent = element.name;
prodPrice.textContent = "₹ " + element.price;
addBtn.textContent = "+ ADD";
addBtn.addEventListener("click", function(){
addToCart(index);
});
addBtn.setAttribute("id", "addBtn");
inBoxDiv2.append(prodTitle, prodPrice, addBtn);
box.setAttribute("class", "prodBox");
box.append(inBoxDiv1, inBoxDiv2);
productsSlider.append(box);
});
productsSlider.setAttribute("id","productsSlider");
//left div ends here now right Div is starting from here.
//freeOrderBtn
if(totalSum<=1000){
freeOrderBtn.innerHTML = "Free For Orders Above 1000 >"
freeOrderBtn.addEventListener("click", function(){
window.location.href = "#";
})
freeOrderBtn.setAttribute("id", "freeOrderBtn");
rightDiv.append(freeOrderBtn);
}
//applyCouponBtn
applyCouponBtn.innerHTML = '<img src="https://www.pulseplus.in/content/images/icons/sale.png"/><p>Apply Coupon</p>';
applyCouponBtn.addEventListener("click", function(){
//code for applyCouponBtn
var couponCode = prompt("Please Put Coupon Code Here And Click on Ok");
if(couponCode=="masai30"){
localStorage.setItem("couponCode", couponCode);
showCartData(cartData, extraProd);
}else{
alert("Coupon Code Is Not Valid")
}
});
applyCouponBtn.setAttribute("id", "applyCouponBtn");
rightDiv.append(applyCouponBtn);
//orderSummaryDiv
var table = document.createElement("table");
var thead = document.createElement("thead");
var tr1 = document.createElement("tr");
var th11 = document.createElement("td");
var tbody = document.createElement("tbody");
var tr2 = document.createElement("tr");
var td21 = document.createElement("td");
var td22 = document.createElement("td");
var tr3 = document.createElement("tr");
var td31 = document.createElement("td");
var td32 = document.createElement("td");
var tr4 = document.createElement("tr");
var td41 = document.createElement("td");
var td42 = document.createElement("td");
var tr5 = document.createElement("tr");
var td51 = document.createElement("td");
var td52 = document.createElement("td");
if(localStorage.getItem("couponCode")!=="masai30"){
var finalCheckOutValue = totalSum + 15;
th11.innerHTML = "<b>Order Summary</b>";
tr1.append(th11);
thead.append(tr1);
td21.innerHTML = "Items";
td22.innerHTML = "₹ "+totalSum+".00";
tr2.append(td21, td22);
td31.innerHTML = "Processing Fee"
td32.innerHTML = "₹ 15.00"
tr3.append(td31, td32);
td41.innerHTML = "<b>Total</b>";
td42.innerHTML = "₹ "+finalCheckOutValue+".00";
tr4.append(td41, td42);
td51.innerHTML = "Discount Applied";
td52.innerHTML = "0 %";
tr5.append(td51, td52);
tbody.append(tr2, tr3, tr5, tr4);
table.append(thead, tbody);
orderSummaryDiv.setAttribute("id","orderSummaryDiv")
orderSummaryDiv.append(table);
rightDiv.append(orderSummaryDiv);
}else{
var finalCheckOutValue = Math.floor(totalSum*0.7 + 15);
th11.innerHTML = "<b>Order Summary</b>";
tr1.append(th11);
thead.append(tr1);
td21.innerHTML = "Items";
td22.innerHTML = "₹ "+(totalSum)+".00";
tr2.append(td21, td22);
td31.innerHTML = "Processing Fee"
td32.innerHTML = "₹ 15.00"
tr3.append(td31, td32);
td41.innerHTML = "<b>Total</b>";
td42.innerHTML = "₹ "+finalCheckOutValue+".00";
tr4.append(td41, td42);
td51.innerHTML = "Discount Applied";
td52.innerHTML = "30 %";
tr5.append(td51, td52);
tbody.append(tr2, tr3, tr5, tr4);
table.append(thead, tbody);
orderSummaryDiv.setAttribute("id","orderSummaryDiv")
orderSummaryDiv.append(table);
rightDiv.append(orderSummaryDiv);
}
//proccedBtn
proceedBtn.textContent = "PROCEED";
proceedBtn.setAttribute("id", "proceedBtn");
proceedBtn.addEventListener("click", function(){
window.location.href = "deliverydetails.html"
})
rightDiv.append(proceedBtn);
//appending sub parts to the main elements.
leftDiv.append(cartProducts, addMedicineBtn, productsSlider);
mainDiv.append(leftDiv, rightDiv);
container.append(pageHeading, mainDiv);
}
}
function checkIfAdded(item, cartData){
for(i=0; i<cartData.length; i++){
if(cartData[i]===item){
return true;
}
}
}
function addToCart(index){
var isPresent = false;
cartData.map(function(element){
if(extraProd[index]===element){
isPresent = true;
}
});
if(isPresent){
alert("The Product Is Already In Cart. You Can Increase Its Quantity There.")
}else{
extraProd[index].quantity = 1;
cartData.push(extraProd[index]);
alert("The Product Is Successfully Added In Cart");
localStorage.setItem("cartData", JSON.stringify(cartData));
showCartData(cartData, extraProd);
}
}
function increaseQuantity(index){
cartData[index].quantity++;
localStorage.setItem("cartData", JSON.stringify(cartData));
showCartData(cartData, extraProd);
}
function decreaseQuantity(index){
if(cartData[index].quantity<=0){
deleteFromCart(index)
}else{
cartData[index].quantity--;
localStorage.setItem("cartData", JSON.stringify(cartData));
showCartData(cartData, extraProd);
}
}
function deleteFromCart(index){
//writ code to delete element from cart.
cartData.splice(index, 1);
localStorage.setItem("cartData", JSON.stringify(cartData));
showCartData(cartData, extraProd);
}
//navBar Js
document.getElementById("navLocation").addEventListener("click", function(){
window.location.href = "#locationSelector";
})