38571-vm/cart.php
Flatlogic Bot 9b7ccbdd3d versionone
2026-02-18 16:23:45 +00:00

101 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/layout.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$productId = isset($_POST['product_id']) ? (int) $_POST['product_id'] : 0;
$quantity = isset($_POST['quantity']) ? (int) $_POST['quantity'] : 1;
$action = isset($_POST['action']) ? (string) $_POST['action'] : 'add';
if ($productId > 0) {
if ($action === 'update') {
cart_update($productId, $quantity);
flash_set('success', 'Cart updated.');
} else {
cart_add($productId, $quantity);
flash_set('success', 'Item added to cart.');
}
}
header('Location: /cart.php');
exit;
}
$items = cart_items();
$total = cart_total();
render_header('Cart - E-SO9', 'cart');
?>
<main class="container my-5">
<h1 class="h3 mb-3">Your cart</h1>
<?php if (!$items): ?>
<div class="alert alert-light border">Your cart is empty. Browse products to get started.</div>
<a href="/shop.php" class="btn btn-outline-secondary">Go to shop</a>
<?php else: ?>
<div class="row g-4">
<div class="col-lg-8">
<div class="table-responsive">
<table class="table align-middle">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Total</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($items as $item): ?>
<tr>
<td>
<div class="fw-semibold"><?= e($item['product']['name']) ?></div>
<div class="text-muted small"><?= e($item['product']['category_name'] ?? 'General') ?></div>
</td>
<td><?= e(format_price((float) $item['product']['price'])) ?></td>
<td>
<form method="post" action="/cart.php" class="d-flex gap-2">
<input type="hidden" name="action" value="update" />
<input type="hidden" name="product_id" value="<?= e((string) $item['product']['id']) ?>" />
<input type="number" name="quantity" class="form-control form-control-sm" min="1" value="<?= e((string) $item['quantity']) ?>" />
<button class="btn btn-outline-secondary btn-sm" type="submit">Update</button>
</form>
</td>
<td><?= e(format_price((float) $item['line_total'])) ?></td>
<td>
<form method="post" action="/cart.php">
<input type="hidden" name="action" value="update" />
<input type="hidden" name="product_id" value="<?= e((string) $item['product']['id']) ?>" />
<input type="hidden" name="quantity" value="0" />
<button class="btn btn-link text-danger" type="submit">Remove</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<div class="col-lg-4">
<div class="stat-card">
<div class="d-flex justify-content-between mb-2">
<span class="text-muted">Subtotal</span>
<span class="fw-semibold"><?= e(format_price((float) $total)) ?></span>
</div>
<div class="d-flex justify-content-between mb-2">
<span class="text-muted">Shipping</span>
<span class="fw-semibold">$0.00</span>
</div>
<div class="d-flex justify-content-between border-top pt-2">
<span class="fw-semibold">Total</span>
<span class="fw-semibold"><?= e(format_price((float) $total)) ?></span>
</div>
<a href="/checkout.php" class="btn btn-primary w-100 mt-3">Proceed to checkout</a>
</div>
</div>
</div>
<?php endif; ?>
</main>
<?php render_footer(); ?>