-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.html
126 lines (113 loc) · 3.84 KB
/
cart.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="styles2.css">
<style>
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table th, table td {
border: 1px solid #ccc;
padding: 10px;
text-align: center;
}
.total {
font-weight: bold;
font-size: 1.2em;
text-align: right;
}
#checkoutButton {
margin-top: 20px;
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
</style>
</head>
<body>
<header>
<h1>Your Cart</h1>
<nav>
<a href="customer.html">Continue Shopping</a>
</nav>
</header>
<main>
<section id="cartSection">
<table>
<thead>
<tr>
<th>Book Title</th>
<th>Price</th>
<th>Quantity</th>
<th>Item Total</th>
<th>Action</th>
</tr>
</thead>
<tbody id="cartTableBody">
<!-- Cart items will be dynamically populated -->
</tbody>
</table>
<div class="total">
Total Price: $<span id="totalPrice">0</span>
</div>
<button id="checkoutButton">Proceed to Checkout</button>
</section>
</main>
<footer>
<p>Book Automation Software © 2024</p>
</footer>
<script>
// Function to render the cart items from localStorage
const renderCart = () => {
const cartTableBody = document.getElementById('cartTableBody');
const totalPriceElement = document.getElementById('totalPrice');
const cart = JSON.parse(localStorage.getItem('cart')) || [];
let total = 0;
cartTableBody.innerHTML = '';
cart.forEach((item, index) => {
const price = parseFloat(item.price.replace('$', ''));
const quantity = parseInt(item.quantity, 10);
const itemTotal = quantity * price;
total += itemTotal;
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.title}</td>
<td>${item.price}</td>
<td>${item.quantity}</td>
<td>$${itemTotal.toFixed(2)}</td>
<td>
<button class="removeItem" data-index="${index}">Remove</button>
</td>
`;
cartTableBody.appendChild(row);
});
totalPriceElement.textContent = total.toFixed(2);
// Add event listener to remove items
document.querySelectorAll('.removeItem').forEach(button => {
button.addEventListener('click', (e) => {
const index = e.target.getAttribute('data-index');
cart.splice(index, 1);
localStorage.setItem('cart', JSON.stringify(cart));
renderCart();
});
});
};
// Redirect to Checkout Page
document.getElementById('checkoutButton').addEventListener('click', () => {
window.location.href = 'checkout.html';
});
// Load cart on page load
document.addEventListener('DOMContentLoaded', renderCart);
</script>
</body>
</html>