-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory.php
More file actions
254 lines (227 loc) · 12.7 KB
/
Copy pathcategory.php
File metadata and controls
254 lines (227 loc) · 12.7 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/**
* Category Products Browser with Sidebar Filtering
*/
require_once __DIR__ . '/includes/helpers.php';
$catSlug = clean($_GET['slug'] ?? '');
try {
$category = null;
if (!empty($catSlug)) {
$catStmt = db()->prepare("SELECT * FROM `categories` WHERE `slug` = ? AND `status` = 'active' LIMIT 1");
$catStmt->execute([$catSlug]);
$category = $catStmt->fetch();
}
// Dynamic Filter Parameters
$priceMin = isset($_GET['price_min']) ? (double)$_GET['price_min'] : 0;
$priceMax = isset($_GET['price_max']) ? (double)$_GET['price_max'] : 2000;
$brandId = isset($_GET['brand']) ? (int)$_GET['brand'] : 0;
$vendorId = isset($_GET['vendor']) ? (int)$_GET['vendor'] : 0;
$rating = isset($_GET['rating']) ? (int)$_GET['rating'] : 0;
$sortBy = clean($_GET['sort'] ?? 'newest');
$inStockOnly = isset($_GET['instock']) ? 1 : 0;
// Base SQL query
$sql = "SELECT p.*, c.name as category_name, b.name as brand_name, vp.store_name
FROM `products` p
LEFT JOIN `categories` c ON p.category_id = c.id
LEFT JOIN `brands` b ON p.brand_id = b.id
LEFT JOIN `vendor_profiles` vp ON p.vendor_id = vp.user_id
WHERE p.status = 'active'";
$params = [];
// Filter by Category or its Children
if ($category) {
// Find child category IDs if it is a parent
$subStmt = db()->prepare("SELECT `id` FROM `categories` WHERE `parent_id` = ? AND `status` = 'active'");
$subStmt->execute([$category['id']]);
$subIds = $subStmt->fetchAll(PDO::FETCH_COLUMN);
if (!empty($subIds)) {
$subIds[] = $category['id'];
$inQuery = implode(',', array_fill(0, count($subIds), '?'));
$sql .= " AND p.category_id IN ($inQuery)";
foreach ($subIds as $id) {
$params[] = $id;
}
} else {
$sql .= " AND p.category_id = ?";
$params[] = $category['id'];
}
}
// Filter by Price range
$sql .= " AND (CASE WHEN p.discount_price IS NOT NULL THEN p.discount_price ELSE p.price END) >= ?
AND (CASE WHEN p.discount_price IS NOT NULL THEN p.discount_price ELSE p.price END) <= ?";
$params[] = $priceMin;
$params[] = $priceMax;
// Filter by Brand
if ($brandId > 0) {
$sql .= " AND p.brand_id = ?";
$params[] = $brandId;
}
// Filter by Vendor
if ($vendorId > 0) {
$sql .= " AND p.vendor_id = ?";
$params[] = $vendorId;
}
// Filter by In stock
if ($inStockOnly > 0) {
$sql .= " AND p.stock > 0";
}
// Sorting
switch ($sortBy) {
case 'price_asc':
$sql .= " ORDER BY (CASE WHEN p.discount_price IS NOT NULL THEN p.discount_price ELSE p.price END) ASC";
break;
case 'price_desc':
$sql .= " ORDER BY (CASE WHEN p.discount_price IS NOT NULL THEN p.discount_price ELSE p.price END) DESC";
break;
case 'newest':
default:
$sql .= " ORDER BY p.created_at DESC";
break;
}
// Execute query
$stmt = db()->prepare($sql);
$stmt->execute($params);
$products = $stmt->fetchAll();
// Fetch Brands for sidebar filter
$brandsList = db()->query("SELECT * FROM `brands` WHERE `status` = 'active'")->fetchAll();
// Fetch Vendors for sidebar filter
$vendorsList = db()->query("SELECT * FROM `vendor_profiles` WHERE `status` = 'active'")->fetchAll();
} catch (Exception $e) {
$products = [];
$brandsList = [];
$vendorsList = [];
}
$pageTitle = $category ? $category['name'] . " - Explore Products" : "All Products";
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/includes/navbar.php';
?>
<div class="container my-4">
<div class="row">
<!-- Sidebar Filters Column -->
<div class="col-lg-3 mb-4">
<div class="card border-0 shadow-sm p-4 rounded-4 sticky-lg-top" style="top: 100px; z-index: 100;">
<h5 class="fw-bold mb-4 border-bottom pb-2"><i class="bi bi-funnel-fill text-primary"></i> Filter Results</h5>
<form action="" method="GET">
<?php if ($category): ?>
<input type="hidden" name="slug" value="<?php echo htmlspecialchars($category['slug']); ?>">
<?php endif; ?>
<!-- Price Range -->
<div class="mb-4">
<label class="form-label fw-bold small text-uppercase">Price Range</label>
<div class="row g-2 mb-2">
<div class="col-6">
<input type="number" name="price_min" class="form-control form-control-sm" placeholder="Min" value="<?php echo $priceMin; ?>">
</div>
<div class="col-6">
<input type="number" name="price_max" class="form-control form-control-sm" placeholder="Max" value="<?php echo $priceMax; ?>">
</div>
</div>
</div>
<!-- Brand selection -->
<div class="mb-4">
<label class="form-label fw-bold small text-uppercase">Brand</label>
<select name="brand" class="form-select form-select-sm">
<option value="0">All Brands</option>
<?php foreach ($brandsList as $b): ?>
<option value="<?php echo $b['id']; ?>" <?php echo $brandId === (int)$b['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($b['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<!-- Vendor selection -->
<div class="mb-4">
<label class="form-label fw-bold small text-uppercase">Vendor</label>
<select name="vendor" class="form-select form-select-sm">
<option value="0">All Vendors</option>
<?php foreach ($vendorsList as $v): ?>
<option value="<?php echo $v['user_id']; ?>" <?php echo $vendorId === (int)$v['user_id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($v['store_name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<!-- In Stock Switch -->
<div class="form-check form-switch mb-4">
<input class="form-check-input" type="checkbox" name="instock" id="instock-switch" value="1" <?php echo $inStockOnly ? 'checked' : ''; ?>>
<label class="form-check-label small fw-semibold" for="instock-switch">In Stock Only</label>
</div>
<button type="submit" class="btn btn-primary w-100 btn-sm fw-bold">Apply Filters</button>
<a href="category.php<?php echo $category ? '?slug=' . $category['slug'] : ''; ?>" class="btn btn-outline-secondary w-100 btn-sm fw-bold mt-2">Reset Filters</a>
</form>
</div>
</div>
<!-- Products Grid Column -->
<div class="col-lg-9">
<!-- Header bar / Sorting -->
<div class="card border-0 shadow-sm p-3 rounded-4 mb-4 d-flex flex-md-row justify-content-between align-items-center gap-3">
<div>
<h4 class="fw-bold mb-0 text-dark"><?php echo $category ? htmlspecialchars($category['name']) : 'All Marketplace Products'; ?></h4>
<span class="text-secondary small"><?php echo count($products); ?> Product(s) Found</span>
</div>
<div class="d-flex align-items-center gap-2">
<label class="small text-muted text-nowrap mb-0">Sort By:</label>
<select class="form-select form-select-sm" style="width: auto;" onchange="location = this.value;">
<?php
$baseQuery = $_GET;
unset($baseQuery['sort']);
$queryString = http_build_query($baseQuery);
$urlPrefix = 'category.php?' . ($queryString ? $queryString . '&' : '');
?>
<option value="<?php echo $urlPrefix; ?>sort=newest" <?php echo $sortBy === 'newest' ? 'selected' : ''; ?>>Newest Arrivals</option>
<option value="<?php echo $urlPrefix; ?>sort=price_asc" <?php echo $sortBy === 'price_asc' ? 'selected' : ''; ?>>Price: Low to High</option>
<option value="<?php echo $urlPrefix; ?>sort=price_desc" <?php echo $sortBy === 'price_desc' ? 'selected' : ''; ?>>Price: High to Low</option>
</select>
</div>
</div>
<!-- Products List -->
<?php if (empty($products)): ?>
<div class="card border-0 shadow-sm p-5 text-center rounded-4">
<div class="fs-1 text-muted mb-3"><i class="bi bi-search-heart"></i></div>
<h5>No products match your criteria.</h5>
<p class="text-secondary small">Try widening your search terms or adjusting the filters.</p>
</div>
<?php else: ?>
<div class="row g-4">
<?php foreach ($products as $p):
$price = $p['price'];
$discountPrice = $p['discount_price'];
$savePercent = $discountPrice ? round((($price - $discountPrice) / $price) * 100) : 0;
?>
<div class="col-md-4 col-sm-6">
<div class="card h-100 product-card shadow-sm border-0 rounded-4">
<?php if ($savePercent > 0): ?>
<span class="position-absolute top-0 start-0 badge bg-danger m-3 rounded-pill">-<?php echo $savePercent; ?>%</span>
<?php endif; ?>
<a href="<?php echo BASE_URL; ?>product.php?slug=<?php echo $p['slug']; ?>" class="text-decoration-none">
<div class="product-img-wrapper rounded-top-4">
<img src="<?php echo BASE_URL; ?>uploads/products/<?php echo htmlspecialchars($p['image']); ?>" onerror="this.src='https://placehold.co/300x300?text=Product';" alt="<?php echo htmlspecialchars($p['name']); ?>">
</div>
</a>
<div class="card-body d-flex flex-column p-3">
<span class="text-muted small"><?php echo htmlspecialchars($p['category_name'] ?? 'General'); ?></span>
<h6 class="card-title fw-bold my-1 text-truncate">
<a href="<?php echo BASE_URL; ?>product.php?slug=<?php echo $p['slug']; ?>" class="text-decoration-none text-dark"><?php echo htmlspecialchars($p['name']); ?></a>
</h6>
<div class="d-flex justify-content-between align-items-center mt-auto">
<div>
<?php if ($discountPrice): ?>
<span class="fw-bold text-danger fs-5"><?php echo format_price($discountPrice); ?></span>
<span class="text-decoration-line-through text-muted small ms-1"><?php echo format_price($price); ?></span>
<?php else: ?>
<span class="fw-bold text-dark fs-5"><?php echo format_price($price); ?></span>
<?php endif; ?>
</div>
<button type="button" class="btn btn-outline-primary btn-sm add-to-cart-btn" data-product-id="<?php echo $p['id']; ?>">
<i class="bi bi-cart-plus"></i>
</button>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>