-
Notifications
You must be signed in to change notification settings - Fork 3
/
cart.js
251 lines (197 loc) · 6.57 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
var arr = JSON.parse(localStorage.getItem("foodData")) || [];
arr.map(function (ele, index) {
var main = document.createElement("div");
var div = document.createElement("div");
var img = document.createElement("img");
img.src = ele.src;
var h3 = document.createElement("h3");
h3.innerText = ele.name;
var plus = document.createElement("div");
plus.innerText = "+";
plus.style.width = "25px";
plus.style.height = "25px";
plus.style.border = "2px solid black";
plus.style.borderRadius = "50%";
plus.style.textAlign = "center";
plus.style.marginTop = "auto";
plus.style.boxSizing = "borderbox";
plus.style.marginLeft = "100px";
plus.style.backgroundColor = "#2A6DFF";
plus.style.color = "#fff";
plus.style.fontSize = "24px";
plus.style.fontWeight = "500";
plus.style.cursor = "pointer";
plus.addEventListener("click", function () {
addTocard(ele);
display();
});
div.append(img, h3, ele.cal);
main.append(div, plus);
document.getElementById("list").append(main);
function addTocard(ele) {
ele.quant = 1;
Cart1.push(ele);
localStorage.setItem("Cart1", JSON.stringify(Cart1));
}
});
var Cart1 = JSON.parse(localStorage.getItem("Cart1")) || [];
display();
function display() {
document.getElementById("cart1").innerHTML = "";
Cart1.map(function (ele, index) {
var maindiv = document.createElement("div");
var img1 = document.createElement("img");
img1.src = ele.src;
var h31 = document.createElement("h6");
h31.innerText = ele.name;
var addremove = document.createElement("div");
var plus1 = document.createElement("button");
plus1.innerText = "+";
plus1.addEventListener("click", function () {
increase();
});
var minus = document.createElement("button");
minus.innerText = "-";
minus.addEventListener("click", function () {
decrease();
});
var qty = document.createElement("div");
qty.innerText = 0 + ele.quant;
addremove.append(plus1, minus, qty);
document.getElementById("cart1").append(img1, h31, addremove);
function increase() {
// console.log("No");
qty.innerText = ele.quant++;
localStorage.setItem("Cart1", JSON.stringify(Cart1));
display();
}
function decrease() {
if (qty.innerText > 1 && ele.quant > 1) {
qty.innerText = ele.quant--;
localStorage.setItem("Cart1", JSON.stringify(Cart1));
display();
} else if (qty.innerText == 1 && ele.quant == 1) {
alert("quanity is already minimum");
alert("Do you want to remove item");
}
}
document.getElementById("total").innerText = Math.round(
Cart1.reduce(function (acc, ele) {
var totalpriceofcart = acc + ele.quant * 11.79
var praect = [{ key1: totalpriceofcart }];
// console.log(praect)
localStorage.setItem("pricecarry", JSON.stringify(praect));
return acc + ele.quant * 11.79 ;
}, 0)
);
document.getElementById("totalitems").innerText = Cart1.reduce(function(acc,ele) {
return acc + ele.quant ;
},0);
});
}
var arr = JSON.parse(localStorage.getItem("foodData")) || [];
function search() {
document.getElementById("list").innerHTML = "";
var searchText = document.getElementById("select").value;
if (searchText == "All Meals") {
// allMeals();
// console.log(searchText);
arr.map(function (ele, index) {
var main = document.createElement("div");
var div = document.createElement("div");
var img = document.createElement("img");
img.src = ele.src;
var h3 = document.createElement("h3");
h3.innerText = ele.name;
var plus = document.createElement("div");
plus.innerText = "+";
plus.style.width = "25px";
plus.style.height = "25px";
plus.style.border = "2px solid black";
plus.style.borderRadius = "50%";
plus.style.textAlign = "center";
plus.style.marginTop = "auto";
plus.style.boxSizing = "borderbox";
plus.style.marginLeft = "100px";
plus.style.backgroundColor = "#2A6DFF";
plus.style.color = "#fff";
plus.style.fontSize = "24px";
plus.style.fontWeight = "500";
plus.style.cursor = "pointer";
plus.addEventListener("click", function () {
addTocard(ele);
display();
});
div.append(img, h3, ele.cal);
main.append(div, plus);
document.getElementById("list").append(main);
function addTocard(ele) {
ele.quant = 1;
Cart1.push(ele);
localStorage.setItem("Cart1", JSON.stringify(Cart1));
}
});
} else {
var searchText = document.getElementById("select").value;
console.log(searchText);
var filterData = arr.filter(function (ele) {
return ele.category == searchText;
// console.log()
});
// console.log(arr);
console.log(filterData);
filterData.map(function (ele, index) {
var main = document.createElement("div");
var div = document.createElement("div");
var img = document.createElement("img");
img.src = ele.src;
var h3 = document.createElement("h3");
h3.innerText = ele.name;
var plus = document.createElement("div");
plus.innerText = "+";
plus.style.width = "25px";
plus.style.height = "25px";
plus.style.border = "2px solid black";
plus.style.borderRadius = "50%";
plus.style.textAlign = "center";
plus.style.marginTop = "auto";
plus.style.boxSizing = "borderbox";
plus.style.marginLeft = "100px";
plus.style.backgroundColor = "#2A6DFF";
plus.style.color = "#fff";
plus.style.fontSize = "24px";
plus.style.fontWeight = "500";
plus.style.cursor = "pointer";
plus.addEventListener("click", function () {
addTocard(ele);
display();
});
div.append(img, h3, ele.cal);
main.append(div, plus);
document.getElementById("list").append(main);
function addTocard(ele) {
ele.quant = 1;
Cart1.push(ele);
localStorage.setItem("Cart1", JSON.stringify(Cart1));
}
});
}
}
function reset() {
Cart1 = [];
localStorage.removeItem("Cart1");
document.getElementById("cart1").innerHTML = "";
document.getElementById("total").innerText = 0;
document.getElementById("totalitems").innerText = "0";
var praect = [{ key1: 0 }];
// console.log(praect)
localStorage.setItem("pricecarry", JSON.stringify(praect));
}
function qwerty(){
if(document.getElementById("cart1").innerHTML == ""){
alert("Please add product to cart for checkout")
}
else{
window.location.href='checkout.html';
}
}