-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
174 lines (153 loc) · 7.8 KB
/
Copy pathcart.php
File metadata and controls
174 lines (153 loc) · 7.8 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
<?php
session_start();
// Database Connection
$conn = mysqli_connect("localhost", "root", "", "clothingstore_db");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 1. ADD TO BAG LOGIC
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['product_id'])) {
$p_id = $_POST['product_id'];
if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); }
if (isset($_SESSION['cart'][$p_id])) { $_SESSION['cart'][$p_id] += 1; }
else { $_SESSION['cart'][$p_id] = 1; }
header("Location: cart.php");
exit();
}
// 2. UPDATE QUANTITY LOGIC
if (isset($_POST['update_qty'])) {
$p_id = $_POST['product_id'];
$new_qty = (int)$_POST['quantity'];
if ($new_qty > 0) {
$_SESSION['cart'][$p_id] = $new_qty;
} else {
unset($_SESSION['cart'][$p_id]);
}
header("Location: cart.php");
exit();
}
// 3. REMOVE FROM BAG
if (isset($_GET['remove'])) {
unset($_SESSION['cart'][$_GET['remove']]);
header("Location: cart.php");
exit();
}
$subtotal = 0;
$shipping = 60.00;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bag | Pastimes®</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
:root { --studio-blue: #0678eb; }
body { font-family: 'Inter', sans-serif; background-color: #fff; }
.navbar-brand { font-weight: 800; font-size: 24px; color: #000 !important; letter-spacing: 1px; text-transform: uppercase; }
.cart-header { padding: 40px 0 20px; border-bottom: 2px solid #000; margin-bottom: 30px; text-transform: uppercase; font-weight: 900; }
.item-price, .accent-price { color: var(--studio-blue); font-weight: 800; }
/* Qty Styling */
.qty-input { width: 60px; border: 1px solid #000; text-align: center; font-weight: bold; height: 35px; border-radius: 0; }
.btn-update { font-size: 11px; padding: 0 15px; text-transform: uppercase; font-weight: 700; border: 1px solid #000; background: #000; color: #fff; height: 35px; transition: 0.3s; }
.btn-update:hover { background: #333; }
/* 100% Width Summary Styling */
.summary-container { background: #f9f9f9; border-top: 4px solid #000; padding: 40px; width: 100%; }
.btn-checkout {
background: var(--studio-blue);
color: #fff;
width: 100%;
padding: 22px;
font-weight: 900;
text-transform: uppercase;
border: none;
letter-spacing: 3px;
font-size: 14px;
transition: 0.4s ease;
}
.btn-checkout:hover { background: #000; transform: translateY(-2px); }
footer { background-color: #000; padding: 60px 0 40px; color: #fff; margin-top: 80px; }
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg border-bottom bg-white">
<div class="container">
<a class="navbar-brand" href="index.php">Pastimes®</a>
<a href="listings.php" class="text-dark small fw-bold text-uppercase text-decoration-none">Continue Shopping</a>
</div>
</nav>
<div class="container mb-5">
<div class="cart-header"><h1>Shopping Bag</h1></div>
<div class="row">
<div class="col-12">
<?php
if (!empty($_SESSION['cart'])):
foreach ($_SESSION['cart'] as $id => $qty):
$id = mysqli_real_escape_string($conn, $id);
$res = mysqli_query($conn, "SELECT * FROM products WHERE id = '$id'");
$item = mysqli_fetch_assoc($res);
if($item):
$total = $item['price'] * $qty;
$subtotal += $total;
?>
<!-- Individual Item Row -->
<div class="d-flex align-items-center border-bottom py-4">
<img src="<?php echo htmlspecialchars($item['image_path']); ?>" style="width: 100px; height: 130px; object-fit: cover;" onerror="this.src='https://via.placeholder.com/100x130?text=Product'">
<div class="px-4 flex-grow-1">
<h4 class="fw-bold text-uppercase mb-2" style="font-size:16px;"><?php echo htmlspecialchars($item['product_name']); ?></h4>
<!-- Quantity Control -->
<form action="cart.php" method="POST" class="d-flex align-items-center gap-2 mb-3">
<input type="hidden" name="product_id" value="<?php echo $id; ?>">
<label class="small text-muted text-uppercase fw-bold">Qty:</label>
<input type="number" name="quantity" value="<?php echo $qty; ?>" min="1" class="qty-input">
<button type="submit" name="update_qty" class="btn-update">Update</button>
</form>
<a href="cart.php?remove=<?php echo $id; ?>" class="text-danger small fw-bold text-decoration-none text-uppercase">Remove Item</a>
</div>
<div class="item-price fs-4">R <?php echo number_format($total, 2); ?></div>
</div>
<?php endif; endforeach; ?>
<!-- 100% WIDTH SUMMARY SECTION -->
<div class="mt-5">
<div class="summary-container">
<h5 class="fw-bold text-uppercase mb-4" style="letter-spacing: 1px;">Order Summary</h5>
<div class="d-flex justify-content-between mb-3">
<span class="text-muted">Subtotal</span>
<span class="fw-bold">R <?php echo number_format($subtotal, 2); ?></span>
</div>
<div class="d-flex justify-content-between mb-3">
<span class="text-muted">Shipping (Standard)</span>
<span class="fw-bold">R <?php echo number_format($shipping, 2); ?></span>
</div>
<hr class="my-4">
<div class="d-flex justify-content-between fs-2 fw-bold mb-5">
<span class="text-uppercase">Total Amount</span>
<span class="accent-price">R <?php echo number_format($subtotal + $shipping, 2); ?></span>
</div>
<!-- FULL WIDTH BUTTON BELOW -->
<a href="checkout.php" class="text-decoration-none">
<button class="btn-checkout">Proceed to Secure Checkout →</button>
</a>
</div>
</div>
<?php else: ?>
<div class="text-center py-5">
<h3 class="text-uppercase fw-light">Your bag is empty</h3>
<a href="listings.php" class="btn btn-dark mt-3 px-5 py-3 rounded-0 fw-bold">GO SHOPPING</a>
</div>
<?php endif; ?>
</div>
</div>
</div>
<footer>
<div class="container text-center">
<h5 class="mb-2 text-uppercase" style="letter-spacing: 2px;">Pastimes</h5>
<div class="pt-3" style="border-top: 1px solid #333; max-width: 340px; margin: 0 auto;">
<p class="mb-1" style="font-size: 9px; text-transform: uppercase; letter-spacing: 2px; color: #aaa;">Digital Architecture & Direction</p>
<p class="mb-0"><a href="#" class="text-white text-decoration-none fw-bold" style="letter-spacing: 4px; font-size: 12px;">AMO MATLHAGA & Kea Masole</a></p>
</div>
</div>
</footer>
</body>
</html>