34968-vm/cart.php
Flatlogic Bot a6434721b7 V5
2025-10-15 00:36:03 +00:00

68 lines
2.4 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$user_id = $_SESSION['user_id'] ?? null;
$session_id = session_id();
$pdoconnection = db();
// Fetch cart items
if ($user_id) {
$stmt = $pdoconnection->prepare("SELECT c.id, mi.name, mi.price, c.quantity, r.name as restaurant_name FROM cart c JOIN menu_items mi ON c.menu_item_id = mi.id JOIN restaurants r ON mi.restaurant_id = r.id WHERE c.user_id = :user_id");
$stmt->bindParam(':user_id', $user_id);
} else {
$stmt = $pdoconnection->prepare("SELECT c.id, mi.name, mi.price, c.quantity, r.name as restaurant_name FROM cart c JOIN menu_items mi ON c.menu_item_id = mi.id JOIN restaurants r ON mi.restaurant_id = r.id WHERE c.session_id = :session_id");
$stmt->bindParam(':session_id', $session_id);
}
$stmt->execute();
$cartItems = $stmt->fetchAll(PDO::FETCH_ASSOC);
$totalPrice = 0;
include 'header.php';
?>
<div class="container mt-5">
<h2 class="text-center mb-4">Your Shopping Cart</h2>
<?php if (count($cartItems) > 0): ?>
<table class="table">
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
<th scope="col">Total</th>
</tr>
</thead>
<tbody>
<?php foreach ($cartItems as $item): ?>
<?php
$itemTotal = $item['price'] * $item['quantity'];
$totalPrice += $itemTotal;
?>
<tr>
<td><?php echo htmlspecialchars($item['name']); ?></td>
<td>$<?php echo number_format($item['price'], 2); ?></td>
<td><?php echo $item['quantity']; ?></td>
<td>$<?php echo number_format($itemTotal, 2); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="text-end">
<h4>Subtotal: $<?php echo number_format($totalPrice, 2); ?></h4>
<a href="checkout.php" class="btn btn-primary mt-3">Proceed to Checkout</a>
</div>
<?php else: ?>
<div class="text-center">
<p>Your cart is empty.</p>
<a href="index.php" class="btn btn-primary">Continue Shopping</a>
</div>
<?php endif; ?>
</div>
<?php require_once 'footer.php'; ?>