-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
311 lines (223 loc) · 8.53 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
var pr=0;
// Cart data
var data1=[{discount: "30",
img_url: "https://img.tatacliq.com/images/i7/437Wx649H/MP000000009068833_437Wx649H_202103132244211.jpeg",
link: "P1.html",
name: "ID Men's Brown Casual Boots",
price: "₹1886",
rating: "3",
type: "ID"},
{discount: "30",
img_url: "https://img.tatacliq.com/images/i7/437Wx649H/MP000000009068833_437Wx649H_202103132244211.jpeg",
link: "P1.html",
name: "ID Men's Brown Casual Boots",
price: "₹1886",
rating: "3",
type: "ID"},{discount: "30",
img_url: "https://img.tatacliq.com/images/i7/437Wx649H/MP000000009068833_437Wx649H_202103132244211.jpeg",
link: "P1.html",
name: "ID Men's Brown Casual Boots",
price: "₹1886",
rating: "3",
type: "ID"}]
// Parsing data to localstorage
var data1=localStorage.setItem("try1",JSON.stringify(data1));
// Accesing data from localstorage
var data2=JSON.parse(localStorage.getItem("cartlist"));
//Total Payment amount box
var totalbox=document.createElement("div");
totalbox.setAttribute("id","totalbox");
var outerdiv=document.getElementById("hh");
// Mapping each cart item
data2.map(function(item, index){
// Creating maindiv for each entry
var maindiv=document.createElement("div");
maindiv.setAttribute("id","maindiv");
// Product image
var image=document.createElement("div")
var pic=document.createElement("img")
pic.setAttribute("src",item.img_url)
//Appending in div
image.append(pic)
image.style.width="33%"
pic.setAttribute("width","100%")
image.style.overflow="hidden"
// Creating div for product details
var text=document.createElement("div")
// Title of product
var title=document.createElement("p")
title.textContent=item.name
// Price of product
var price=document.createElement("p")
price.textContent=item.price
// Quantity button
var qty=document.createElement("select")
qty.setAttribute("id","qunty")
// var opt0=document.createElement("option")
// opt0.textContent="----"
var opt1=document.createElement("option")
opt1.textContent="1"
var opt2=document.createElement("option")
opt2.textContent="2"
var opt3=document.createElement("option")
opt3.textContent="3"
var opt4=document.createElement("option")
opt4.textContent="4"
// try
var lmw=price.textContent
lmw=lmw.split("")
lm1=lmw.shift()
lmw=lmw.join("")
// console.log(+(lmw))
pr+=+lmw
// qty.addEventListener("change",function(){
// for(var z=0; z<data2.length; z++){
// // console.log(data2[z].price)
// // console.log(qty[z].value)
// }
// })
document.getElementById("totalprice").innerHTML=pr;
var kk=0
qty.addEventListener("change",function() {
if(Number(qty.value)) {
var netprice=0;
if(qty.value!==c) {
q=qty.value
a
var a=price.textContent
b=a.split("")
var c=b.shift()
var d=b.join("")
console.log(b)
var npr=Number(q-1)*Number(d)
console.log(npr)
// pr+=Number(q-1)*Number(d)
}
document.getElementById("totalprice").textContent=pr+npr;
document.getElementById("subtotal").textContent=pr+npr;}
})
// console.log(qty.value)
// Color option
var color=document.createElement("p")
color.textContent="color:brown"
// Size option
var size=document.createElement("p")
size.textContent="Size: UK/IND-7"
size.style.display="inline-block"
qty.append(opt1,opt2,opt3,opt4)
// Creating a hr tag
var hr=document.createElement("hr")
hr.style.backgroundColor="grey"
// Creating break
var br=document.createElement("br")
// Creating Save to wishlist and remove tag
var book=document.createElement("img");
book.setAttribute("src","https://cdn-icons-png.flaticon.com/512/3440/3440293.png");
book.style.cursor="pointer";
// book.addEventListener("click",function(){
// })
book.setAttribute("width","10%");
book.style.display="inline-block";
// Creating Save to wishlist text
var wishlist=document.createElement("p");
wishlist.textContent="Save to Wishlist";
wishlist.style.display="inline-block";
// Creating Remove button
var remove=document.createElement("p")
remove.innerHTML="Remove"
remove.setAttribute("id","remove")
//bybharat
remove.addEventListener("click", function(){
makeDelete(index)
})
//appending to text div
text.append(title,price,qty,color,size,hr,br,book,wishlist,remove)
text.style.width="34%"
text.style.paddingLeft="5%"
// Creating delivery icons and text
var delivery=document.createElement("div");
// Master div with display-inline block
var div1=document.createElement("div");
div1.style.display="flex"
// delvery iconn div
var icon=document.createElement("img")
icon.setAttribute("src","https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkJ7J8fstcAZkc0bJeOUeShS929ESxb1ORhcfd6pCL8uCZw5k1F6-1pglfEX2G4hwIFrg&usqp=CAU")
icon.setAttribute("width","80px")
// Date to be delivered
var date=document.createElement("p")
date.innerHTML="Delivery by 11th Nov | "
// Appending to the master div
div1.append(icon,date)
// Appending to delivery div
delivery.append(div1)
// appending 3 main flex divs to maindiv
maindiv.append(image,text,delivery);
// Appending maindiv to outer most div
var outerdiv=document.getElementById("contain");
outerdiv.append(maindiv);
// Continue shooping
// document.getElementById("continueshop").addEventListener("click",gotohome);
// Remmove cart function
remove.addEventListener("click",cartremove);
function cartremove(item){
data2.splice(item,1);
maindiv.style.display="none";
console.log(data2);
}
function gotohome() {
window.location.href="index.html";
}
})
var btn=document.createElement("button")
btn.textContent="Continue Shopping"
btn.setAttribute("id","continueshop")
document.getElementById("hh").append(btn)
btn.addEventListener("click",gotohome)
function gotohome() {
window.location.href="index.html"
}
// console.log(netprice)
document.getElementById("totalprice").textContent
// console.log(pr)
// Continue button
document.getElementById("Checkout").addEventListener("click",movetopayment)
function movetopayment() {
var finalrate=document.getElementById("totalprice")
localStorage.setItem("bagtotal",finalrate.textContent)
window.location.href="checkout.html"
}
// // ADDRESS
// var add=JSON.parse(localStorage.getItem("address"))
// console.log(add)
// var showadd=add[1]+","+add[4]
// document.getElementById("addr").textContent=showadd
// coupon
document.getElementById("codecheck").addEventListener("click",showcode)
var count=0
function showcode() {
if(count==0) {
var code=document.createElement("input");
code.placeholder="Enter Code Here";
code.setAttribute("id","codebox");
document.getElementById("codeinput").append(code);
var apply=document.createElement("button");
apply.textContent="Apply now";
document.getElementById("btn").append(apply);
count++}
}
// var apply=document.createElement("button")
// apply.textContent="Apply"
// document.getElementById("codeinput").append(apply)
// var t1=document.getElementById("codebox").value
// console.log(t1)
function makeDelete(index) {
data2.splice(index, 1)
localStorage.setItem("cartlist", JSON.stringify(data2));
}
// // Home
document.getElementById("nav2logo").addEventListener("click",function(){
window.location.href="index.html"
})
var add=JSON.parse(localStorage.getItem("address"))
console.log(add)
document.getElementById("addr").textContent=`${add[1]}`+ " ,"+`${add[add.length-1]}`