120 lines
5.3 KiB
PHP
120 lines
5.3 KiB
PHP
<?php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
require_once 'products_data.php';
|
|
|
|
// Handle quantity updates
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_cart'])) {
|
|
foreach ($_POST['quantities'] as $productId => $quantity) {
|
|
$quantity = (int)$quantity;
|
|
if ($quantity > 0) {
|
|
$_SESSION['cart'][$productId] = $quantity;
|
|
} else {
|
|
unset($_SESSION['cart'][$productId]);
|
|
}
|
|
}
|
|
header('Location: cart.php');
|
|
exit;
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Shopping Cart - EcoGrow</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&family=Roboto:wght@400;500&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
|
|
<?php include 'includes/header.php'; ?>
|
|
|
|
<header class="bg-success text-white text-center py-5">
|
|
<div class="container">
|
|
<h1 class="display-4 fw-bold">Your Shopping Cart</h1>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container my-5">
|
|
<section id="cart-items">
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<?php if (empty($_SESSION['cart'])): ?>
|
|
<div class="alert alert-info text-center">
|
|
<p class="mb-0">Your cart is empty.</p>
|
|
<a href="products.php" class="btn btn-primary mt-3">Continue Shopping</a>
|
|
</div>
|
|
<?php else: ?>
|
|
<form action="cart.php" method="post">
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Product</th>
|
|
<th>Price</th>
|
|
<th style="width: 120px;">Quantity</th>
|
|
<th>Total</th>
|
|
<th>Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
$total = 0;
|
|
foreach ($_SESSION['cart'] as $productId => $quantity) {
|
|
$product = null;
|
|
foreach ($products as $p) {
|
|
if ($p['id'] == $productId) {
|
|
$product = $p;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($product) {
|
|
$subtotal = $product['price'] * $quantity;
|
|
$total += $subtotal;
|
|
echo '<tr>';
|
|
echo '<td>' . htmlspecialchars($product['name']) . '</td>';
|
|
echo '<td>$' . htmlspecialchars($product['price']) . '</td>';
|
|
echo '<td>';
|
|
echo '<input type="number" name="quantities[' . $productId . ']" value="' . htmlspecialchars($quantity) . '" class="form-control form-control-sm" min="1">';
|
|
echo '</td>';
|
|
echo '<td>$' . htmlspecialchars(number_format($subtotal, 2)) . '</td>';
|
|
echo '<td><a href="remove_from_cart.php?id=' . $productId . '" class="btn btn-danger btn-sm">Remove</a></td>';
|
|
echo '</tr>';
|
|
}
|
|
}
|
|
?>
|
|
</tbody>
|
|
<tfoot>
|
|
<tr>
|
|
<th colspan="3" class="text-end">Grand Total:</th>
|
|
<th colspan="2">$<?php echo htmlspecialchars(number_format($total, 2)); ?></th>
|
|
</tr>
|
|
</tfoot>
|
|
</table>
|
|
<div class="d-flex justify-content-between">
|
|
<a href="products.php" class="btn btn-primary">Continue Shopping</a>
|
|
<div>
|
|
<button type="submit" name="update_cart" class="btn btn-info">Update Cart</button>
|
|
<a href="#" class="btn btn-success">Proceed to Checkout</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
|
|
<footer class="bg-dark text-white pt-5 pb-3">
|
|
<div class="container text-center">
|
|
<p class="mb-0">© <?php echo date("Y"); ?> EcoGrow. All Rights Reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|