80 lines
3.1 KiB
PHP
80 lines
3.1 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
include 'header.php';
|
|
|
|
$cart_items = [];
|
|
$total_price = 0;
|
|
|
|
if (!empty($_SESSION['cart'])) {
|
|
$menu_item_ids = array_keys($_SESSION['cart']);
|
|
$placeholders = implode(',', array_fill(0, count($menu_item_ids), '?'));
|
|
|
|
$stmt = db()->prepare("SELECT * FROM menu_items WHERE id IN ($placeholders)");
|
|
$stmt->execute($menu_item_ids);
|
|
$db_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
foreach ($db_items as $item) {
|
|
$quantity = $_SESSION['cart'][$item['id']];
|
|
$item_total = $item['price'] * $quantity;
|
|
$total_price += $item_total;
|
|
$cart_items[] = [
|
|
'id' => $item['id'],
|
|
'name' => $item['name'],
|
|
'price' => $item['price'],
|
|
'quantity' => $quantity,
|
|
'total' => $item_total
|
|
];
|
|
}
|
|
}
|
|
?>
|
|
|
|
<main>
|
|
<div class="container">
|
|
<h1>Your Cart</h1>
|
|
<?php if (empty($cart_items)): ?>
|
|
<p>Your cart is empty.</p>
|
|
<?php else: ?>
|
|
<table class="cart-table">
|
|
<thead>
|
|
<tr>
|
|
<th>Item</th>
|
|
<th>Price</th>
|
|
<th>Quantity</th>
|
|
<th>Total</th>
|
|
<th>Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($cart_items as $item): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($item['name']) ?></td>
|
|
<td>$<?= htmlspecialchars(number_format($item['price'], 2)) ?></td>
|
|
<td>
|
|
<form action="cart_actions.php" method="POST" class="update-form">
|
|
<input type="hidden" name="action" value="update">
|
|
<input type="hidden" name="menu_item_id" value="<?= $item['id'] ?>">
|
|
<input type="number" name="quantity" value="<?= $item['quantity'] ?>" min="1" class="quantity-input">
|
|
<button type="submit" class="update-btn">Update</button>
|
|
</form>
|
|
</td>
|
|
<td>$<?= htmlspecialchars(number_format($item['total'], 2)) ?></td>
|
|
<td>
|
|
<a href="cart_actions.php?action=remove&menu_item_id=<?= $item['id'] ?>" class="remove-link">Remove</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<div class="cart-total">
|
|
<h3>Total: $<?= htmlspecialchars(number_format($total_price, 2)) ?></h3>
|
|
</div>
|
|
<div class="cart-actions">
|
|
<a href="cart_actions.php?action=clear" class="clear-cart-btn">Clear Cart</a>
|
|
<a href="checkout.php" class="checkout-btn">Proceed to Checkout</a>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</main>
|
|
|
|
<?php include 'footer.php'; ?>
|