-
Notifications
You must be signed in to change notification settings - Fork 1
/
socks-ecommerce-platform.html
66 lines (59 loc) · 2.3 KB
/
socks-ecommerce-platform.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Colorful Socks E-commerce Platform</title>
<link rel="stylesheet" href="socks-ecommerce-styles.css">
</head>
<body>
<h1>Colorful Socks E-commerce Platform</h1>
<div id="products"></div>
<div id="cart"></div>
<button id="checkout">Checkout</button>
<script>
const products = [
{ id: 1, name: 'Red Socks', price: 9.99, image: 'images/red-socks.jpg' },
{ id: 2, name: 'Blue Socks', price: 9.99, image: 'images/blue-socks.jpg' },
{ id: 3, name: 'Green Socks', price: 9.99, image: 'images/green-socks.jpg' }
];
let cart = [];
function displayProducts() {
const productsDiv = document.getElementById('products');
productsDiv.innerHTML = products.map(product => `
<div class="product">
<img src="${product.image}" alt="${product.name}">
<h3>${product.name}</h3>
<p>Price: $${product.price}</p>
<button onclick="addToCart(${product.id})">Add to Cart</button>
</div>
`).join('');
}
function addToCart(productId) {
const product = products.find(p => p.id === productId);
cart.push(product);
updateCart();
}
function updateCart() {
const cartDiv = document.getElementById('cart');
cartDiv.innerHTML = `
<h2>Shopping Cart</h2>
${cart.map(item => `<p>${item.name} - $${item.price}</p>`).join('')}
<p><strong>Total: $${cart.reduce((sum, item) => sum + item.price, 0).toFixed(2)}</strong></p>
`;
}
function checkout() {
if (cart.length === 0) {
alert('Your cart is empty!');
} else {
const total = cart.reduce((sum, item) => sum + item.price, 0).toFixed(2);
alert(`Thank you for your purchase! Total: $${total}`);
cart = [];
updateCart();
}
}
displayProducts();
document.getElementById('checkout').addEventListener('click', checkout);
</script>
</body>
</html>