-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshop.js
88 lines (69 loc) · 2.37 KB
/
shop.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
let cart = [];
function addToCart(name, price, image, size = null, color = null) {
const product = {
name: name,
price: price,
image: image,
size: size,
color: color,
quantity: 1
};
const existingProductIndex = cart.findIndex(item =>
item.name === name && item.size === size && item.color === color
);
if (existingProductIndex >= 0) {
cart[existingProductIndex].quantity += 1;
} else {
cart.push(product);
}
updateCartSummary();
showNotification(`${name} added to cart.`);
}
function updateCartSummary() {
const cartSummary = document.getElementById('cart-summary');
cartSummary.innerHTML = '';
if (cart.length === 0) {
cartSummary.innerHTML = '<p>No items in cart.</p>';
return;
}
const list = document.createElement('ul');
let total = 0;
cart.forEach((product, index) => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<img src="images/${product.image}" alt="${product.name}" width="50">
${product.name} (${product.size}, ${product.color}) - $${product.price} x ${product.quantity}
<button onclick="removeFromCart(${index})">Remove</button>
`;
list.appendChild(listItem);
total += product.price * product.quantity;
});
const totalItem = document.createElement('li');
totalItem.innerHTML = `Total: $${total}`;
list.appendChild(totalItem);
cartSummary.appendChild(list);
}
function removeFromCart(index) {
cart.splice(index, 1);
updateCartSummary();
}
function checkout() {
if (cart.length === 0) {
alert('Your cart is empty. Please add items to your cart before proceeding to checkout.');
return;
}
alert('Thank you for your support.Proceeding to checkout...');
}
function showNotification(message) {
const notification = document.getElementById('notification');
notification.textContent = message;
setTimeout(() => {
notification.textContent = '';
}, 3000);
}
function scrollToOrderSummary() {
var orderSummary = document.getElementById('order-summary');
if (orderSummary) {
orderSummary.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}