-
Notifications
You must be signed in to change notification settings - Fork 2
/
cart.js
236 lines (139 loc) · 5.08 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
if (document.readyState == 'loading')
{
document.addEventListener("DOMContentLoaded", ready)
} else
{
ready();
}
function ready()
{
var p_mainImgSrc = "";
var p_itemTitle = "";
var p_itemPrice = "";
var p_itemQuantity = "";
// remove cart item - accessing buttons
var removeCartItemsBtn = document.getElementsByClassName("fa-times-circle");
for (var i = 0; i < removeCartItemsBtn.length; i++)
{
var button = removeCartItemsBtn[i];
button.addEventListener('click', removeCartItem);
}
// Quantity change - accesing qty inputs
var quantityInputs = document.getElementsByClassName("cart-quantity-input");
for (var i = 0; i < quantityInputs.length; i++)
{
var input = quantityInputs[i];
input.addEventListener('change', quantityChanged);
input.addEventListener('change', updateSubtotal);
input.addEventListener('change', updateCartTotal);
}
// adding items to cart
var cartBody = document.getElementById("cart-body");
if (cartBody)
{
if (localStorage.getItem('cartData') != null)
{
var myCartData = JSON.parse(localStorage.getItem('cartData'));
for (var i = 0; i < myCartData.length; i++)
{
var itemData = myCartData[i];
var subtotal = Number(itemData[2].replace("₹", "").trim()) * Number(itemData[3]);
var cartRow = document.createElement('tr');
cartRow.classList.add("cart-row");
cartRow.innerHTML = `<td>
<i class="far fa-times-circle"></i>
</td>
<td><img src="${itemData[0]}" alt="" /></td>
<td>${itemData[1]}</td>
<td class="cart-price">${itemData[2]}</td>
<td><input type="number" value="${itemData[3]}" class="cart-quantity-input" min="1"/></td>
<td class="cart-item-subtotal">₹ ${subtotal}</td>`;
var cartItems = document.getElementById("cart-body");
cartItems.appendChild(cartRow);
cartRow.getElementsByClassName("fa-times-circle")[0].addEventListener('click', removeCartItem);
cartRow.getElementsByClassName("cart-quantity-input")[0].addEventListener('change', updateSubtotal);
cartRow.getElementsByClassName("cart-quantity-input")[0].addEventListener('change', quantityChanged);
cartRow.getElementsByClassName("cart-quantity-input")[0].addEventListener('change', updateSubtotal);
cartRow.getElementsByClassName("cart-quantity-input")[0].addEventListener('change', updateCartTotal);
updateCartTotal();
}
}
updateCartTotal();
// purchase btn
var purchaseBtn = document.getElementById("btn-purchase");
purchaseBtn.addEventListener('click', purchaseBtnClicked);
}
}
// update subtotal - function
function updateSubtotal(event)
{
var input = event.target;
var curCartRow = input.parentElement.parentElement;
var curPriceElement = curCartRow.getElementsByClassName("cart-price")[0];
var curPrice = parseFloat(curPriceElement.innerText.replace("₹", "").trim());
var curQuantity = input.value;
var subtotal = curQuantity * curPrice;
curCartRow.getElementsByClassName("cart-item-subtotal")[0].innerText = "₹ " + subtotal;
// var mainImgSrc = document.getElementById("MainImg").src;
}
// Quantity change - function
function quantityChanged(event)
{
var input = event.target;
if (isNaN(input.value) || input.value <= 0)
{
input.value = 1;
}
updateCartTotal();
}
// remove cart item - function
function removeCartItem(event)
{
var buttonClicked = event.target;
var cartRow = buttonClicked.parentElement.parentElement;
var cartTitle = cartRow.getElementsByTagName('td')[2].innerHTML;
var existingCartData = JSON.parse(localStorage.getItem('cartData'));
for (var i = 0; i < existingCartData.length; i++)
{
if (existingCartData[i][1] == cartTitle)
{
existingCartData.splice(i, 1);
}
}
localStorage.setItem('cartData', JSON.stringify(existingCartData));
buttonClicked.parentElement.parentElement.remove();
updateCartTotal();
}
// update cart total - function
function updateCartTotal()
{
var cartItemContainer = document.getElementsByClassName("cart-items")[0];
var cartRows = cartItemContainer.getElementsByClassName("cart-row");
var total = 0;
for (var i = 0; i < cartRows.length; i++)
{
var cartRow = cartRows[i];
var subtotalElement = cartRow.getElementsByClassName("cart-item-subtotal")[0];
var subtotal = parseFloat(subtotalElement.innerText.replace("₹", "").trim());
var total = total + subtotal;
}
total = Math.round(total * 100) / 100;
document.getElementsByClassName("cart-subtotal")[0].innerText = "₹ " + total;
document.getElementsByClassName("cart-total")[0].innerText = "₹ " + total;
}
// Purchase clicked - function
function purchaseBtnClicked(event)
{
if(localStorage.getItem("cartData") != null)
{
localStorage.removeItem('cartData');
alert("Processing your Payment. \nPlease wait. . .")
location.href='loader.html';
}else{
alert("Shopping cart Empty. Please Add Items to Your cart!")
}
}
// coupon invalid
function coupon(){
alert("Please Enter Valid Coupon Code!")
}