-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
140 lines (128 loc) · 5.33 KB
/
cart.php
File metadata and controls
140 lines (128 loc) · 5.33 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
<?php
include_once 'includes/header.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['update_cart'])) {
foreach ($_POST['quantity'] as $cart_id => $quantity) {
if ($quantity <= 0) {
$delete_stmt = $conn->prepare("DELETE FROM Cart WHERE cart_id = ? AND user_id = ?");
$delete_stmt->bind_param("ii", $cart_id, $_SESSION['user_id']);
$delete_stmt->execute();
} else {
$update_stmt = $conn->prepare("UPDATE Cart SET quantity = ? WHERE cart_id = ? AND user_id = ?");
$update_stmt->bind_param("iii", $quantity, $cart_id, $_SESSION['user_id']);
$update_stmt->execute();
}
}
$_SESSION['success'] = "Cart updated successfully.";
header("Location: cart.php");
exit();
} elseif (isset($_POST['remove_item']) && isset($_POST['cart_id'])) {
$cart_id = (int)$_POST['cart_id'];
$delete_stmt = $conn->prepare("DELETE FROM Cart WHERE cart_id = ? AND user_id = ?");
$delete_stmt->bind_param("ii", $cart_id, $_SESSION['user_id']);
$delete_stmt->execute();
$_SESSION['success'] = "Item removed from cart.";
header("Location: cart.php");
exit();
}
}
$sql = "SELECT c.cart_id, c.quantity, p.product_id, p.name, p.price, p.image_url, p.stock
FROM Cart c
JOIN Products p ON c.product_id = p.product_id
WHERE c.user_id = ?
ORDER BY c.added_at DESC";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $_SESSION['user_id']);
$stmt->execute();
$result = $stmt->get_result();
$cart_items = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
$cart_total = 0;
foreach ($cart_items as $item) {
$cart_total += $item['price'] * $item['quantity'];
}
?>
<h1>Your Shopping Cart</h1>
<?php if (isset($_SESSION['success'])): ?>
<div class="alert alert-success"><?php echo $_SESSION['success']; ?></div>
<?php unset($_SESSION['success']); ?>
<?php endif; ?>
<?php if (isset($_SESSION['error'])): ?>
<div class="alert alert-danger"><?php echo $_SESSION['error']; ?></div>
<?php unset($_SESSION['error']); ?>
<?php endif; ?>
<?php if (empty($cart_items)): ?>
<div class="empty-cart">
<p>Your cart is empty.</p>
<a href="products.php" class="btn">Continue Shopping</a>
</div>
<?php else: ?>
<form method="post" action="cart.php">
<table class="cart-table">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($cart_items as $item): ?>
<tr>
<td data-label="Product">
<div class="cart-product">
<img src="assets/images/<?php echo htmlspecialchars($item['image_url']); ?>" alt="<?php echo htmlspecialchars($item['name']); ?>" width="80">
<div>
<h4><?php echo htmlspecialchars($item['name']); ?></h4>
<small>SKU: <?php echo $item['product_id']; ?></small>
</div>
</div>
</td>
<td data-label="Price"><?php echo formatCurrency($item['price']); ?></td>
<td data-label="Quantity">
<input type="number" name="quantity[<?php echo $item['cart_id']; ?>]" value="<?php echo $item['quantity']; ?>" min="0" max="<?php echo $item['stock']; ?>" class="quantity-input">
</td>
<td data-label="Total"><?php echo formatCurrency($item['price'] * $item['quantity']); ?></td>
<td data-label="Actions">
<form method="post" action="cart.php" style="display:inline;">
<input type="hidden" name="cart_id" value="<?php echo $item['cart_id']; ?>">
<button type="submit" name="remove_item" class="btn btn-danger btn-sm">Remove</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="cart-actions">
<button type="submit" name="update_cart" class="btn">Update Cart</button>
<a href="products.php" class="btn btn-secondary">Continue Shopping</a>
</div>
</form>
<div class="cart-summary">
<h3>Cart Summary</h3>
<table>
<tr>
<td>Total Items:</td>
<td><?php echo count($cart_items); ?></td>
</tr>
<tr>
<td>Subtotal</td>
<td><?php echo formatCurrency($cart_total); ?></td>
</tr>
<tr>
<td>Total</td>
<td><?php echo formatCurrency($cart_total); ?></td>
</tr>
</table>
<a href="checkout.php" class="btn">Proceed to Checkout</a>
</div>
<?php endif; ?>
<?php
include_once 'includes/footer.php';
?>