forked from nikhildeora/cool-monk-ecommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
221 lines (184 loc) · 7.56 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
import navbar from "./cmmponents/navbar.js"
const navbar_div = document.getElementById("navbar")
navbar_div.innerHTML = navbar()
import footer from "./cmmponents/footer.js"
const footer_div = document.getElementById("footer")
footer_div.innerHTML = footer()
// function to set items in local storage
// let btn=document.getElementById("cart_btn")
// btn.addEventListener("click",()=>{
// function addItems(el)
// })
// let Addcart=[];
// function addItems(el){
// Addcart.push(el);
// localStorage.setItem("cartitems",JSON.stringify(Addcart))
// }
//function for cart page
let addedItems=JSON.parse(localStorage.getItem("cartitems")) || [];
// console.log(addedItems.length);
// console.log(addedItems);
let sum;
function sum_append(){
sum = 0;
addedItems.forEach(function(el){
sum += Number(el.price)
})
localStorage.setItem("cart_total",sum)
document.getElementById('total').innerText=sum;
}
sum_append()
let container=document.getElementById("container")
const appendItems=(addedItems)=>{
container.innerHTML=null;
addedItems.forEach(function(el,index){
// let div=document.createElement('div')
// let img=document.createElement('img')
// let name=document.createElement('h2')
// let desc=document.createElement('p')
// let price=document.createElement('h2')
// let delete_btn=document.createElement('button')
// let see_details=document.createElement('button')
// let btn_div=document.createElement('div')
// btn_div.id='buttons'
// img.src=el.image;
// name.innerText=el.name;
// desc.innerText=el.shipping;
// price.innerText=el.price
// delete_btn.innerText='Remove From Cart'
// see_details.innerText='See Details'
// see_details.onclick = ()=>{
// localStorage.setItem("swiped_data",JSON.stringify(el))
// window.location.href = "./items_detail.html"
// }
// delete_btn.addEventListener("click",()=>{
// deleteProducts(el,index)
// })
// btn_div.append(delete_btn,see_details)
// div.append(img,name,price,desc,btn_div)
// container.append(div)
let div = document.createElement("div");
div.setAttribute("class", "cat_cards");
let image = document.createElement("img");
image.src = el.image;
let name = document.createElement("h2")
name.innerText = el.name;
let price = document.createElement("p")
price.setAttribute("class", "cat_price")
price.innerText = el.price;
let div_star = document.createElement("div")
for (let i = 0; i < el.star; i++) {
let star = document.createElement("i")
star.setAttribute("class", "fa-solid fa-star")
div_star.append(star)
}
let ship = document.createElement("p");
ship.setAttribute("class", "cat_ship")
ship.innerText = el.shipping;
let btn_div = document.createElement("div")
btn_div.id = "buttons"
let show_detail = document.createElement("button")
show_detail.setAttribute("class","show_detail")
show_detail.innerText = "See Details"
show_detail.onclick = ()=>{
localStorage.setItem("swiped_data",JSON.stringify(el))
window.location.href = "./items_detail.html"
}
let remove_btn = document.createElement("button")
remove_btn.innerText = "Remove"
remove_btn.setAttribute("class","remove_btn")
remove_btn.addEventListener("click",()=>{
deleteProducts(el,index)
})
// btn_div.append(show_detail,remove_btn)
div.append(image, name, price, div_star, ship,show_detail,remove_btn);
container.append(div)
})
}
let total_cart_products = document.getElementById("cartitems")
// total_cart_products.innerText = addedItems.length;
function deleteProducts(el,index)
{
addedItems.splice(index,1)
appendItems(addedItems)
total_cart_products.innerText = addedItems.length;
sum_append()
localStorage.setItem("cartitems",JSON.stringify(addedItems))
}
window.onload = ()=>{
appendItems(addedItems)
total_cart_products.innerText = addedItems.length;
}
// category js
let mens_cat = document.getElementsByClassName("mens")
let womens_cat = document.getElementsByClassName("womens")
let beauty_cat = document.getElementsByClassName("beauty")
let electronics_cat = document.getElementsByClassName("electronics")
let jewellery_cat = document.getElementsByClassName("jewellery")
let kids_cat = document.getElementsByClassName("kids")
let videogame_cat = document.getElementsByClassName("videogame")
for(let i=0;i<mens_cat.length;i++){
mens_cat[i].addEventListener("click",function(){
console.log("nikhil");
localStorage.setItem("category_call","mens")
window.location.href = "category.html"
})
}
for(let i=0;i<womens_cat.length;i++){
womens_cat[i].addEventListener("click",function(){
console.log("nikhil");
localStorage.setItem("category_call","womens")
window.location.href = "category.html"
})
}
for(let i=0;i<beauty_cat.length;i++){
beauty_cat[i].addEventListener("click",function(){
console.log("nikhil");
localStorage.setItem("category_call","beauty")
window.location.href = "category.html"
})
}
for(let i=0;i<electronics_cat.length;i++){
electronics_cat[i].addEventListener("click",function(){
console.log("nikhil");
localStorage.setItem("category_call","electronics")
window.location.href = "category.html"
})
}
for(let i=0;i<jewellery_cat.length;i++){
jewellery_cat[i].addEventListener("click",function(){
console.log("nikhil");
localStorage.setItem("category_call","jewellery")
window.location.href = "category.html"
})
}
for(let i=0;i<kids_cat.length;i++){
kids_cat[i].addEventListener("click",function(){
console.log("nikhil");
localStorage.setItem("category_call","kids")
window.location.href = "category.html"
})
}
for(let i=0;i<videogame_cat.length;i++){
videogame_cat[i].addEventListener("click",function(){
console.log("nikhil");
localStorage.setItem("category_call","videogame")
window.location.href = "category.html"
})
}
// login logout status
let homePage_username_logout_div = document.getElementById("username_logout_homePage_div")
let Username_onHomePage = document.getElementById("Username_onHomePage")
let homePage_logout_btn = document.getElementById("homePage_logout_btn")
let signup_login_homePage_div = document.getElementById("signup_login_homePage_div")
let login_logout_status = JSON.parse(localStorage.getItem("login_logout_status"))
if(login_logout_status.login_status=="loggedin"){
signup_login_homePage_div.style.display = "none";
Username_onHomePage.innerText = login_logout_status.username;
homePage_username_logout_div.style.display = "flex";
}
homePage_logout_btn.onclick = ()=>{
localStorage.removeItem("login_logout_status")
signup_login_homePage_div.style.display = "block";
homePage_username_logout_div.style.display = "none";
}