forked from taruntailor7/fraazo-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.js
171 lines (135 loc) · 5.42 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
var cartProduct = JSON.parse(localStorage.getItem("cart")) || [];
// console.log(cartProduct);
var cart = document.getElementById('cart');
if(cartProduct.length == 0){
displayEmptyProduct();
}
else{
displayProduct(cartProduct);
cartValue();
}
document.getElementById("items").innerText = cartProduct.length;
// document.querySelector('#cart > h1').innerText = "Cart items:" + cartProduct.length;
document.getElementById('items').innerText =cartProduct.length+" items";
document.getElementById('items').style.fontSize="12px";
function cartValue(){
var total = 0;
if(cartProduct.length!=0){
cartProduct.map(function(element){
total += element.price*element.piec;
});
}
else{
total = cartProduct.reduce(function(accumulator, element) {
return accumulator+Number(element.price);
},0);
}
document.getElementById("total").innerText ="₹ "+total;
document.getElementById("totals").innerText ="₹ "+total;
document.getElementById("pay").innerText ="₹ "+total;
document.getElementById("check").innerText ="₹ "+total;
}
// Header Section
var loggedInUser = JSON.parse(localStorage.getItem("userName")) || [];
// console.log(loggedInUser);
document.getElementById("user").innerText =loggedInUser[0].name;
function displayEmptyProduct(){
document.getElementById('cart').style.display = 'none';
document.getElementById('cartValue').style.display = 'none';
var h1 = document.createElement('h1');
h1.innerText = "My Cart (0 Items)";
// var imgDiv = document.createElement('div');
// imgDiv.setAttribute("class", "img");
var image = document.createElement('img');
image.setAttribute("src","https://webasset.fraazo.com/production/empty_cart.35cdf7d10a7af693e2ea.png");
// imgDiv.append(image);
var h2 = document.createElement('h2');
h2.innerText = "Whoops... Cart is empty";
var p = document.createElement('p');
p.setAttribute("class", "underline");
p.innerText = "Add some fruits, veggies and dairy products to your cart.";
// var div = document.createElement('div');
// div.setAttribute("id", "button");
var a = document.createElement('a');
a.setAttribute("href", "homepage.html");
var btn = document.createElement('button');
btn.innerText = "Let's Shop!";
a.append(btn);
// div.append(a);
document.getElementById("empty").append(h1,image,h2,p,a);
}
function displayProduct(prod){
document.getElementById("empty").style.display = "none";
document.getElementById("cart").innerText="";
prod.map(function(element, index){
// var h1 = document.createElement("h1");
// h1.innerText = cartProduct.length;
var div = document.createElement("div");
// div.style.border = "1px solid blue";
var image = document.createElement("img");
image.setAttribute("src", element.image_url);
var mainDiv = document.createElement("div");
mainDiv.setAttribute("class","nameAndQuantityPrice");
// mainDiv.style.border = "1px solid red";
var div1 = document.createElement("div");
// div1.style.border = "1px solid red";
var p = document.createElement("p");
p.innerText = element.name;
var quantity = document.createElement("p");
quantity.innerText = element.quantity;
var price = document.createElement("h4");
price.innerText = "₹ "+ element.price*element.piec; //jsoidghdhrgdd
div1.append(p,quantity,price);
var btnDiv = document.createElement("div");
btnDiv.width = "100%"
// btnDiv.style.border = "1px solid black"
var minus = document.createElement("button");
minus.innerText = "-";
minus.addEventListener("click",function(){
decrease(index);
refreshPage();
});
var pieces = document.createElement("p");
pieces.innerText = element.piec;
pieces.setAttribute("id", "decVal")
pieces.style.marginTop = "80px"
var plus = document.createElement("button");
plus.innerText = "+";
plus.addEventListener("click",function(){
increase(index);
refreshPage();
});
btnDiv.append(minus, pieces, plus,);
var btn = document.createElement("button");
btn.innerText = "Remove";
btn.addEventListener("click", function(){
remove(index);
refreshPage(); // this is for refreshing the page because when we remove last one page should refresh and then show empty cart.
});
mainDiv.append(div1, btnDiv, btn);
div.append(image,mainDiv);
document.getElementById("cart").append(div);
});
}
function remove(index){
cartProduct.splice(index,1);
localStorage.setItem("cart",JSON.stringify(cartProduct));
displayProduct(cartProduct);
}
function refreshPage(){
window.location.reload();
}
function decrease(index){
cartProduct[index].piec = cartProduct[index].piec - 1;
if(cartProduct[index].piec == 0){
remove(index);
}
localStorage.setItem("cart",JSON.stringify(cartProduct));
displayProduct(cartProduct);
}
function increase(index){
cartProduct[index].piec = cartProduct[index].piec + 1;
localStorage.setItem("cart",JSON.stringify(cartProduct));
displayProduct(cartProduct);
}
// console.log(cartProduct);