-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
276 lines (243 loc) · 10.9 KB
/
index.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
var k = 0;
var itemsInCart = 0;
if (document.readyState == "loading") {
document.addEventListener("DOMContentLoaded", ready)
} else {
ready();
}
function ready() {
var removeCartItemButtons = document.getElementsByClassName("remove");
for (var i = 0; i < removeCartItemButtons.length; i++) {
var button = removeCartItemButtons[i];
button.addEventListener('click', removeCartItem)
}
var increaseValue = document.getElementsByClassName("increaseValue")
for (var i = 0; i < increaseValue.length; i++) {
var button = increaseValue[i];
button.addEventListener('click', increaseQuantity)
}
var decreaseValue = document.getElementsByClassName("decreaseValue")
for (var i = 0; i < decreaseValue.length; i++) {
var button = decreaseValue[i];
button.addEventListener('click', decreaseQuantity)
}
var addToCartButtons = document.getElementsByClassName("shop-item-button");
for (var i = 0; i < addToCartButtons.length; i++) {
var button = addToCartButtons[i];
button.addEventListener('click', addToCartClicked);
}
document.getElementsByClassName('btn-checkout')[0].addEventListener('click', checkoutClicked);
document.getElementsByClassName('btn-clear-cart')[0].addEventListener('click', clearCartClicked);
updateCartTotal();
}
function clearCartClicked() {
itemsInCart = 0;
k = 0;
alert("Your Cart will be cleared!");
var cartItems = document.getElementsByClassName('cart-items')[0];
while (cartItems.hasChildNodes()) {
cartItems.removeChild(cartItems.firstChild);
}
updateCartTotal();
}
function checkoutClicked() {
itemsInCart = 0;
k = 0;
alert("Thank you for your purchase");
var cartItems = document.getElementsByClassName('cart-items')[0];
while (cartItems.hasChildNodes()) {
cartItems.removeChild(cartItems.firstChild);
}
updateCartTotal();
}
function updateCartTotal() {
var total = 0;
var cartItemContainer = document.getElementsByClassName("cart-items")[0];
var cartRows = cartItemContainer.getElementsByClassName("cart-item");
for (var i = 0; i < cartRows.length; i++) {
var currRow = cartRows[i];
var priceElement = currRow.getElementsByClassName("price")[0];
var quantityElement = currRow.getElementsByClassName("quantity")[0];
var everyItemTotal = currRow.getElementsByClassName("curr-item-total")[0];
var price = parseFloat(priceElement.innerText);
var quantity = quantityElement.value;
everyItemTotal.innerHTML = price * quantity;
total = total + (price * quantity);
}
total = Math.round(total * 100) / 100;
var productTotalAmount = document.getElementById('productTotalAmount');
productTotalAmount.innerHTML = total + '.00';
var shippingCharges = document.getElementById('shippingCharges');
if (total >= 500) {
shippingCharges.innerHTML = 0 + '.00';
} else {
shippingCharges.innerHTML = 50 + '.00';
}
let discount = document.getElementById('discount');
if (k == 1) {
let productAmount = document.getElementById('productTotalAmount');
temp = parseInt(productAmount.innerText);
discount.innerHTML = temp / 5 + '.00';
}
var finalCartAmount = document.getElementById('finalTotalAmount');
var finalAmount = parseInt(total) + parseInt(shippingCharges.innerHTML) - parseInt(discount.innerHTML);
finalCartAmount.innerHTML = finalAmount + ".00";
var badgeNav = document.getElementById('no-of-items-nav');
var badgeNav2 = document.getElementById('no-of-items-nav2');
var badgeHead = document.getElementById('no-of-items-head');
badgeNav.innerText = itemsInCart;
badgeNav2.innerText = itemsInCart;
badgeHead.innerText = itemsInCart;
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var today = new Date();
var time = today.getHours()+2 + ":" + today.getMinutes() + ":" + today.getSeconds();
document.getElementById('delievery-date').innerText = 'Date: ' + date + ' ' + 'Time: ' + time;
}
function removeCartItem(event) {
itemsInCart--;
var buttonClicked = event.target;
var removed = buttonClicked.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.getElementsByClassName('cart-item-title');
console.log(removed);
var removedTitle = removed.innerText;
console.log(removedTitle);
buttonClicked.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.remove();
updateCartTotal();
}
function increaseQuantity(event) {
var buttonClicked = event.target;
var parent = buttonClicked.parentElement.parentElement.parentElement;
var quantityElement = parent.getElementsByClassName("quantity")[0];
var quantity = quantityElement.value;
quantityElement.value = parseInt(quantity) + 1;
if (parseInt(quantity) >= 5) {
quantityElement.value = 5;
alert("You cannot take more than 5 products");
}
Color(quantityElement);
updateCartTotal();
}
function decreaseQuantity(event) {
var buttonClicked = event.target;
var parent = buttonClicked.parentElement.parentElement.parentElement;
var quantityElement = parent.getElementsByClassName("quantity")[0];
var quantity = quantityElement.value;
quantityElement.value = parseInt(quantity) - 1;
if ((parseInt(quantity) - 1) <= 0) {
quantityElement.value = 1;
alert("Minimum 1 product needs to be chosen");
}
Color(quantityElement);
updateCartTotal();
}
function Color(element) {
if (element.value == 1) {
element.style.color = 'blue';
}
else if (element.value == 5) {
element.style.color = 'red';
}
else {
element.style.color = 'black';
}
}
function addToCartClicked(event) {
itemsInCart++;
var button = event.target;
var shopItem = button.parentElement.parentElement.parentElement.parentElement.parentElement;
var title = shopItem.getElementsByClassName("shop-item-title")[0].innerText;
var price = shopItem.getElementsByClassName("shop-item-price")[0].innerText;
var imageSrc = shopItem.getElementsByClassName("shop-item-image")[0].src;
var button = shopItem.getElementsByClassName("shop-item-button")[0];
addItemToCart(title, price, imageSrc, button);
updateCartTotal();
}
function addItemToCart(title, price, imageSrc, button) {
var cartRow = document.createElement('div');
var cartItems = document.getElementsByClassName("cart-items")[0];
var cartItemsNames = cartItems.getElementsByClassName("cart-item-title");
var btn = button;
for (var i = 0; i < cartItemsNames.length; i++) {
if (cartItemsNames[i].innerText == title) {
alert("This item is already added");
return;
}
}
var cartRowContents = `
<div class="cart-item shadow mb-2">
<div class="card p-4">
<div class="row">
<!-- Cart Images div -->
<div class="col-md-5 col-11 mx-auto bg-light d-flex
justify-content-center align-items-center shadow product_image">
<img src="${imageSrc}" alt="cart img" class="img-fluid">
</div>
<!-- Cart product details -->
<div class="col-md-7 col-11 mx-auto px-4 mt-2">
<div class="row">
<!-- product name -->
<div class="col-6 card_title">
<h1 class="mb-4 product_name cart-item-title">${title}</h1>
<p>Fresh ${title}</p>
<p>Price per piece - <span class="price">${price}</span></p>
</div>
<!-- quantity increment decrement -->
<div class="col-6">
<ul class="pagination justify-content-end set_quantity">
<li class="page-item">
<button class="page-link decreaseValue">
<i class="fas fa-minus"></i>
</button>
</li>
<li class="page-item">
<input type="text" name="" class="page-link quantity" value="1"
disabled>
</li>
<li class="page-item">
<button class="page-link increaseValue">
<i class="fas fa-plus"></i>
</button>
</li>
</ul>
</div>
</div>
<!-- remove move and price -->
<div class="row">
<div class="col-8 d-flex justify-content-between remove_wish">
<button class="btn-sm btn-outline-danger remove"><i
class="fas fa-trash"></i> REMOVE ITEM</button>
<!-- <p><i class="fas fa-heart"></i>Move to wish list</p> -->
</div>
<div class="col-4 d-flex justify-content-end price_money">
<h3>₹<span class="curr-item-total">0.00</span></h3>
</div>
</div>
</div>
</div>
</div>
</div>`
btn.style.backgroundColor = 'green';
btn.innerText = "Added";
cartRow.innerHTML = cartRowContents;
cartItems.append(cartRow);
cartRow.getElementsByClassName("remove")[0].addEventListener('click', removeCartItem);
cartRow.getElementsByClassName("increaseValue")[0].addEventListener('click', increaseQuantity);
cartRow.getElementsByClassName("decreaseValue")[0].addEventListener('click', decreaseQuantity);
}
const discount_code = () => {
let errorThrow = document.getElementById('errorThrow');
if (k == 1) {
errorThrow.innerHTML = "Code Already Applied";
}
else if (discountCode.value === 'Delicious' && k == 0) {
let productAmount = document.getElementById('productTotalAmount');
let discount = document.getElementById('discount');
temp = parseInt(productAmount.innerText);
discount.innerHTML = temp / 5 + '.00';
errorThrow.innerHTML = "Hurray! code is valid";
k = 1;
updateCartTotal();
} else {
errorThrow.innerHTML = "Try Again! Valid code is Delicious";
}
}