-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
135 lines (102 loc) · 3.58 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
let Namearr = JSON.parse(localStorage.getItem("username")) ;
Namearr.map(function(ele){
document.getElementById("login_text").innerText=ele.name;
})
// coupon
let total=0;
let disc=0;
let coupons=200;
document.querySelector("#promobtn").addEventListener("click",applycoupon)
function applycoupon(){
if(coupons==200){
let per=total*0.30;
disc=total-per;
document.querySelector("#saving_show").innerText="Congrat!!, You have got 30% Discount";
// alert()
document.querySelector("#totalprice").innerText=disc;
total=disc;
}
}
var loadcart = JSON.parse(localStorage.getItem("cartlist"))
var loadwishlist=JSON.parse(localStorage.getItem('wishlistItem'))||[];
let quat=0;
let sum=0;
loadcart.map(function(elem,index){
quat=quat+elem.counter;
document.querySelector("#quantity").innerText=quat;
document.getElementById("item").innerText=quat+" ";
sum=sum+Number(elem.price)*elem.counter;
total=sum;
document.querySelector("#totalprice").innerText=total;
let div=document.createElement("div");
let div2=document.createElement("div");
div2.setAttribute("class","card");
let img=document.createElement("img");
img.setAttribute("src",elem.image_url);
let h2=document.createElement("h2");
h2.innerText=elem.head;
let p=document.createElement("p");
p.innerText=elem.name;
let h3=document.createElement("h2");
h3.setAttribute("id","price");
h3.innerText=+elem.price;
let h4=document.createElement("p");
h4.innerText="You saved ₹650!"
h4.style.color="teal";
// opt.createElement("option");
let p2=document.createElement("p");
p2.innerText="Quantity:-"+" "+elem.counter;
var array1 = ["SIZE","S","M","L"];
let selectlist1=document.createElement("select");
selectlist1.id = "mySelect";
for (var i = 0; i < array1.length; i++) {
var option = document.createElement("option");
option.value = array1[i];
option.text = array1[i];
selectlist1.appendChild(option);
}
var array2 = ["QTY",1,2,3,4,5,6,7,8,9];
let selectlist2=document.createElement("select");
selectlist2.id = "mySelect2";
for (var i = 0; i < array2.length; i++) {
var option = document.createElement("option");
option.value = array2[i];
option.text = array2[i];
selectlist2.appendChild(option);
}
let remove=document.createElement("button");
remove.setAttribute("id","delete_btn")
remove.innerText="remove";
remove.addEventListener("click",function(){
deleteitem(index)})
let wishlist=document.createElement("button");
wishlist.setAttribute("id","wishlist_btn")
wishlist.innerText="Move to wishlist";
wishlist.addEventListener("click",function(){
movetowishlist(elem,index)})
let hr=document.createElement("hr")
div2.append(h2,p,h3,h4, p2,selectlist1,selectlist2,hr,remove,wishlist);
div.append(div2,img)
document.querySelector("#cartitem").append(div);
});
document.querySelector("#placeorderbtn").addEventListener("click",pricestore);
function pricestore(){
let totalamt=[{total:total}];
localStorage.setItem("pricestore",JSON.stringify(totalamt))
window.location.href="address.html"
}
function deleteitem(index){
// alert(" working on delete ")
loadcart.splice(index,1)
localStorage.setItem("cartlist",JSON.stringify(loadcart))
window.location.reload();
}
function movetowishlist(elem,index){
loadwishlist.push(elem);
localStorage.setItem("wishlistItem",JSON.stringify(loadwishlist))
localStorage.setItem("cartlist",JSON.stringify(loadcart))
loadcart.splice(index,1)
localStorage.setItem("cartlist",JSON.stringify(loadcart))
alert("Added To Wishlist")
window.location.reload()
}