207 lines
8.9 KiB
PHP
207 lines
8.9 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// Initialize basket if it doesn't exist
|
|
if (!isset($_SESSION['basket'])) {
|
|
$_SESSION['basket'] = [];
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Handle actions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|
$productId = $_POST['product_id'] ?? null;
|
|
|
|
switch ($_POST['action']) {
|
|
case 'add':
|
|
if ($productId) {
|
|
// Check if product exists and is in stock
|
|
$stmt = $pdo->prepare('SELECT name, stock_quantity FROM products WHERE id = ?');
|
|
$stmt->execute([$productId]);
|
|
$product = $stmt->fetch();
|
|
|
|
if ($product && $product['stock_quantity'] > 0) {
|
|
if (isset($_SESSION['basket'][$productId])) {
|
|
// Prevent adding more than available stock
|
|
if ($_SESSION['basket'][$productId] < $product['stock_quantity']) {
|
|
$_SESSION['basket'][$productId]++;
|
|
} else {
|
|
$_SESSION['message']['warning'] = "Cannot add more of this item. Not enough stock for " . $product['name'];
|
|
}
|
|
} else {
|
|
$_SESSION['basket'][$productId] = 1;
|
|
}
|
|
} else {
|
|
$_SESSION['message']['danger'] = "Cannot add item. Not enough stock for " . $product['name'];
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'update':
|
|
$quantity = $_POST['quantity'] ?? 1;
|
|
if ($productId && $quantity > 0) {
|
|
// Check stock
|
|
$stmt = $pdo->prepare('SELECT name, stock_quantity FROM products WHERE id = ?');
|
|
$stmt->execute([$productId]);
|
|
$product = $stmt->fetch();
|
|
if ($product && $quantity <= $product['stock_quantity']) {
|
|
$_SESSION['basket'][$productId] = (int)$quantity;
|
|
} else {
|
|
$_SESSION['message']['danger'] = "Cannot update quantity. Not enough stock for " . $product['name'];
|
|
}
|
|
} else if ($productId && $quantity <= 0) {
|
|
unset($_SESSION['basket'][$productId]);
|
|
}
|
|
break;
|
|
|
|
case 'remove':
|
|
if ($productId) {
|
|
unset($_SESSION['basket'][$productId]);
|
|
}
|
|
break;
|
|
}
|
|
// Redirect to avoid form resubmission
|
|
$redirect_url = $_POST['return_url'] ?? 'basket.php';
|
|
header('Location: ' . $redirect_url);
|
|
exit;
|
|
}
|
|
|
|
// Fetch product details for items in basket
|
|
$basketItems = [];
|
|
$totalPrice = 0;
|
|
if (!empty($_SESSION['basket'])) {
|
|
$productIds = array_keys($_SESSION['basket']);
|
|
$placeholders = implode(',', array_fill(0, count($productIds), '?'));
|
|
|
|
$stmt = $pdo->prepare("SELECT id, name, price, description, stock_quantity FROM products WHERE id IN ($placeholders)");
|
|
$stmt->execute($productIds);
|
|
$products = $stmt->fetchAll(PDO::FETCH_ASSOC | PDO::FETCH_GROUP);
|
|
|
|
foreach ($_SESSION['basket'] as $productId => $quantity) {
|
|
if (isset($products[$productId])) {
|
|
$product = $products[$productId][0];
|
|
$itemPrice = $product['price'] * $quantity;
|
|
$totalPrice += $itemPrice;
|
|
$basketItems[] = [
|
|
'id' => $productId,
|
|
'name' => $product['name'],
|
|
'price' => $product['price'],
|
|
'quantity' => $quantity,
|
|
'stock_quantity' => $product['stock_quantity'],
|
|
'total' => $itemPrice
|
|
];
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Shopping Basket - GiftShop</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
|
|
<!-- Navbar -->
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="index.php">GiftShop</a>
|
|
<div class="collapse navbar-collapse">
|
|
<ul class="navbar-nav ms-auto">
|
|
<li class="nav-item"><a class="nav-link" href="index.php#products">Products</a></li>
|
|
<li class="nav-item"><a class="nav-link" href="index.php#contact">Contact</a></li>
|
|
<li class="nav-item">
|
|
<a class="nav-link active" href="basket.php">
|
|
<i class="bi bi-cart-fill"></i> Basket (<?= array_sum($_SESSION['basket'] ?? []) ?>)
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<!-- Main Content -->
|
|
<main class="container py-5">
|
|
<h1 class="mb-4">Your Shopping Basket</h1>
|
|
|
|
<?php if (isset($_SESSION['message'])):
|
|
foreach ($_SESSION['message'] as $type => $message):
|
|
?>
|
|
<div class="alert alert-<?= $type ?> alert-dismissible fade show" role="alert">
|
|
<?= htmlspecialchars($message) ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php
|
|
endforeach;
|
|
unset($_SESSION['message']);
|
|
endif;
|
|
?>
|
|
|
|
<?php if (empty($basketItems)): ?>
|
|
<div class="alert alert-info text-center">
|
|
<p class="mb-0">Your basket is empty.</p>
|
|
<a href="index.php" class="btn btn-primary mt-3">Start Shopping</a>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="row">
|
|
<div class="col-lg-8">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<?php foreach ($basketItems as $item): ?>
|
|
<div class="row align-items-center mb-3 pb-3 border-bottom">
|
|
<div class="col-md-7">
|
|
<h5><?= htmlspecialchars($item['name']) ?></h5>
|
|
<p class="text-muted mb-0">$<?= number_format($item['price'], 2) ?></p>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<form action="basket.php" method="post" class="d-flex align-items-center">
|
|
<input type="hidden" name="action" value="update">
|
|
<input type="hidden" name="product_id" value="<?= $item['id'] ?>">
|
|
<input type="number" name="quantity" value="<?= $item['quantity'] ?>" min="1" max="<?= $item['stock_quantity'] ?>" class="form-control form-control-sm" style="width: 70px;" onchange="this.form.submit()">
|
|
</form>
|
|
</div>
|
|
<div class="col-md-2 text-end">
|
|
<form action="basket.php" method="post">
|
|
<input type="hidden" name="action" value="remove">
|
|
<input type="hidden" name="product_id" value="<?= $item['id'] ?>">
|
|
<button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-4">
|
|
<div class="card shadow-sm">
|
|
<div class="card-body">
|
|
<h5 class="card-title mb-3">Order Summary</h5>
|
|
<div class="d-flex justify-content-between mb-4">
|
|
<span class="h5">Total</span>
|
|
<span class="h5 text-primary">$<?= number_format($totalPrice, 2) ?></span>
|
|
</div>
|
|
<a href="#" class="btn btn-primary w-100 mb-2">Proceed to Checkout</a>
|
|
<a href="index.php" class="btn btn-outline-secondary w-100">Continue Shopping</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
|
|
<!-- Footer -->
|
|
<footer class="py-4 mt-5 bg-light">
|
|
<div class="container text-center">
|
|
<p class="mb-0">© <?= date("Y") ?> GiftShop. All Rights Reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|