-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
268 lines (218 loc) · 10.3 KB
/
script.js
File metadata and controls
268 lines (218 loc) · 10.3 KB
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
window.onload = function () {
document.documentElement.classList.add('loaded');
if (document.querySelector('.product_slider')) {
new Swiper('.product_slider', {
loop: true,
direction: 'horizontal',
slidesPerView: 1,
spaceBetween: 30,
speed: 1000,
parallax: true,
mouseWheel: true,
keyboard: {
enabled: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
on: {
init: function () {
console.log('initiated');
},
},
});
}
const cart = document.querySelector('.header_cart');
const cartValue = document.querySelector('.header_cart span');
const sideMenu = document.querySelector('.side-menu .cart-content');
const totalDisplay = document.getElementById('amt');
const emptyCartMsg = document.getElementById('emptyCartMsg');
const speedAnimation = 1000;
const cartPos = {
left: sideMenu.getBoundingClientRect().left,
top: sideMenu.getBoundingClientRect().top,
};
let totalAmount = 0;
let totalQuantity = 0;
document.addEventListener('click', function (e) {
const targetElement = e.target;
if (targetElement.classList.contains('product_buy') || targetElement.classList.contains('buy-now')) {
const productSlide = targetElement.closest('.product_slide');
const productTitle = productSlide.querySelector('.product_title').textContent;
const productPrice = parseFloat(productSlide.querySelector('.product_price').textContent.replace('Rs.', ''));
const productImageSrc = productSlide.querySelector('.product_picture').src;
const existingCartItem = findCartItem(productTitle);
if (existingCartItem) {
console.log('incremented');
incrementCartValue(productTitle, productPrice, productImageSrc);
} else {
addToCart(productTitle, productPrice, productImageSrc);
}
const isEmptyCart = sideMenu.childElementCount === 0;
if (isEmptyCart) {
const initialText = sideMenu.querySelector('p');
if (initialText) {
emptyCartMsg.style.display = 'block';
}
}
const productImage = productSlide.querySelector('.product_picture');
const productImageFly = productImage.cloneNode(true);
productImageFly.style.cssText = `
position: fixed;
left: ${productImage.getBoundingClientRect().left}px;
top: ${productImage.getBoundingClientRect().top}px;
width: ${productImage.offsetWidth}px;
height: ${productImage.offsetHeight}px;
transition: all ${speedAnimation}ms ease;
`;
document.body.append(productImageFly);
setTimeout(() => {
productImageFly.style.left = `${cartPos.left}px`;
productImageFly.style.top = `${cartPos.top}px`;
productImageFly.style.width = `0px`;
productImageFly.style.height = `0px`;
productImageFly.style.opacity = `0.5`;
}, 0);
totalQuantity += 1;
totalAmount += productPrice;
} else if (targetElement.classList.contains('remove-item')) {
const cartItem = targetElement.closest('p');
if (cartItem) {
const removedQuantity = parseInt(cartItem.querySelector('.quantity').textContent, 10);
const removedPrice = parseFloat(cartItem.querySelector('.cart-item-price').textContent.replace('Rs.', ''));
cartItem.parentNode.removeChild(cartItem);
totalQuantity -= removedQuantity;
totalAmount -= removedPrice * removedQuantity;
if (sideMenu.childElementCount === 0) {
const initialText = document.createElement('p');
initialText.textContent = 'Your cart is empty.';
sideMenu.appendChild(initialText);
emptyCartMsg.style.display = 'none';
}
}
}
updateHeaderCart();
updateSideMenuTotal();
});
function findCartItem(productTitle) {
return Array.from(sideMenu.querySelectorAll('.cart-item-title')).find(title => title.textContent === productTitle);
}
function incrementCartValue(productTitle, productPrice, productImageSrc) {
const existingCartItem = findCartItem(productTitle);
if (existingCartItem) {
// If the product already exists, increment the quantity by 1
const quantityElement = existingCartItem.querySelector('.quantity');
let quantity = parseInt(quantityElement.textContent, 10);
quantity += 1;
quantityElement.textContent = quantity;
updateCartItemTotal(existingCartItem, quantity, productPrice);
} else {
// If the product does not exist, add it to the cart with a quantity of 1
addToCart(productTitle, productPrice, productImageSrc);
}
// Update the total quantity and amount
totalQuantity += 1;
totalAmount += productPrice;
// Update the header cart and side menu total
updateHeaderCart();
updateSideMenuTotal();
}
function addToCart(productTitle, productPrice, productImageSrc) {
const cartItem = document.createElement('div');
const quantity = 1;
cartItem.innerHTML = `
<p>
<img src="${productImageSrc}" alt="Product Image" class="cart-item-image">
<span class="cart-item-details">
<span class="cart-item-title">${productTitle}</span>
<span class="cart-item-price">Rs.${productPrice.toFixed(2)}</span>
</span>
<span class="quantity-group">
<button class="btn btn-primary">-</button>
<span class="quantity" style="font-size: 1.5rem;">${quantity}</span>
<button class="btn btn-primary">+</button>
</span>
<span class="cart-item-total">Rs.${productPrice.toFixed(2)}</span>
</p>
`;
sideMenu.appendChild(cartItem);
const quantityButtons = cartItem.querySelectorAll('.quantity-group button');
quantityButtons.forEach(button => {
button.addEventListener('click', function () {
const cartItem = this.closest('p');
const quantityElement = cartItem.querySelector('.quantity');
let quantity = parseInt(quantityElement.textContent, 10);
if (this.textContent === '-') {
quantity = Math.max(0, quantity - 1);
quantityElement.textContent = quantity;
} else if (this.textContent === '+') {
quantity += 1;
quantityElement.textContent = quantity;
}
updateCartItemTotal(cartItem, quantity, productPrice);
if (quantity === 0) {
cartItem.parentNode.removeChild(cartItem);
}
totalQuantity = Array.from(sideMenu.querySelectorAll('.quantity')).reduce((sum, el) => sum + parseInt(el.textContent, 10), 0);
totalAmount = Array.from(sideMenu.querySelectorAll('.cart-item-total')).reduce((sum, el) => sum + parseFloat(el.textContent.replace('Rs.', '')), 0);
updateHeaderCart();
updateSideMenuTotal();
});
});
}
function updateHeaderCart() {
const cartValue = document.querySelector('.header_cart span');
cartValue.innerHTML = totalQuantity;
}
function updateSideMenuTotal() {
const sideMenuTotal = document.getElementById('amt');
sideMenuTotal.innerHTML = `Rs.${totalAmount.toFixed(2)}`;
}
function updateCartItemTotal(cartItem, quantity, productPrice) {
const itemTotalPrice = quantity * productPrice;
cartItem.querySelector('.cart-item-total').textContent = `Rs.${itemTotalPrice.toFixed(2)}`;
}
document.getElementById('overlay').addEventListener('click', function () {
toggleOverlay();
});
// Submit button click event
const submitBtn = document.querySelector('.submit-btn');
submitBtn.addEventListener('click', function () {
const cartItems = Array.from(sideMenu.querySelectorAll('.cart-item-title'));
const cartQuantities = Array.from(sideMenu.querySelectorAll('.quantity'));
const cartData = {};
cartItems.forEach((item, index) => {
const productTitle = item.textContent.trim();
const productQuantity = parseInt(cartQuantities[index].textContent, 10);
cartData[productTitle] = productQuantity;
});
// Add the logic for creating the form and submitting it
var form = document.createElement('form');
form.method = 'POST';
form.action = '/create_order';
var hiddenAmountField = document.createElement('input');
hiddenAmountField.type = 'hidden';
hiddenAmountField.name = 'order_amount';
hiddenAmountField.value = totalAmount.toString();
var hiddenQuantityField = document.createElement('input');
hiddenQuantityField.type = 'hidden';
hiddenQuantityField.name = 'order_quantity';
hiddenQuantityField.value = totalQuantity.toString();
var hiddenCartDataField = document.createElement('input');
hiddenCartDataField.type = 'hidden';
hiddenCartDataField.name = 'cart_data';
hiddenCartDataField.value = JSON.stringify(cartData);
form.appendChild(hiddenAmountField);
form.appendChild(hiddenQuantityField);
form.appendChild(hiddenCartDataField);
document.body.appendChild(form);
form.submit();
});
};
function toggleOverlay() {
var overlay = document.getElementById('overlay');
var sideMenu = document.getElementById('sideMenu');
overlay.style.display = (overlay.style.display === 'block') ? 'none' : 'block';
sideMenu.style.right = (sideMenu.style.right === '0px') ? '-1000px' : '0px';
}