98 lines
3.2 KiB
PHP
98 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/includes/bootstrap.php';
|
|
require_once __DIR__ . '/includes/layout.php';
|
|
|
|
$items = cart_items();
|
|
$errors = [];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$name = trim((string) ($_POST['name'] ?? ''));
|
|
$email = trim((string) ($_POST['email'] ?? ''));
|
|
$address = trim((string) ($_POST['address'] ?? ''));
|
|
|
|
if ($name === '') {
|
|
$errors[] = 'Name is required.';
|
|
}
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'Valid email is required.';
|
|
}
|
|
if ($address === '') {
|
|
$errors[] = 'Address is required.';
|
|
}
|
|
if (!$items) {
|
|
$errors[] = 'Your cart is empty.';
|
|
}
|
|
|
|
if (!$errors) {
|
|
$orderNumber = create_order([
|
|
'name' => $name,
|
|
'email' => $email,
|
|
'address' => $address
|
|
], $items);
|
|
|
|
if ($orderNumber) {
|
|
cart_clear();
|
|
flash_set('success', 'Order placed successfully.');
|
|
header('Location: /order.php?order=' . urlencode($orderNumber));
|
|
exit;
|
|
}
|
|
$errors[] = 'Unable to place the order. Try again.';
|
|
}
|
|
}
|
|
|
|
render_header('Checkout - E-SO9', 'cart');
|
|
?>
|
|
<main class="container my-5">
|
|
<h1 class="h3 mb-3">Checkout</h1>
|
|
|
|
<?php foreach ($errors as $error): ?>
|
|
<div class="alert alert-warning"><?= e($error) ?></div>
|
|
<?php endforeach; ?>
|
|
|
|
<div class="row g-4">
|
|
<div class="col-lg-7">
|
|
<div class="stat-card">
|
|
<h2 class="h6 mb-3">Customer details</h2>
|
|
<form method="post" action="/checkout.php">
|
|
<div class="mb-3">
|
|
<label class="form-label">Full name</label>
|
|
<input type="text" name="name" class="form-control" value="<?= e((string) ($_POST['name'] ?? '')) ?>" required />
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Email</label>
|
|
<input type="email" name="email" class="form-control" value="<?= e((string) ($_POST['email'] ?? '')) ?>" required />
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Shipping address</label>
|
|
<textarea name="address" class="form-control" rows="3" required><?= e((string) ($_POST['address'] ?? '')) ?></textarea>
|
|
</div>
|
|
<button class="btn btn-primary" type="submit">Place demo order</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
<div class="col-lg-5">
|
|
<div class="stat-card">
|
|
<h2 class="h6 mb-3">Order summary</h2>
|
|
<?php if (!$items): ?>
|
|
<p class="text-muted">Add items to cart to continue.</p>
|
|
<?php else: ?>
|
|
<ul class="list-unstyled mb-3">
|
|
<?php foreach ($items as $item): ?>
|
|
<li class="d-flex justify-content-between mb-2">
|
|
<span><?= e($item['product']['name']) ?> x <?= e((string) $item['quantity']) ?></span>
|
|
<span><?= e(format_price((float) $item['line_total'])) ?></span>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
<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) cart_total())) ?></span>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
<?php render_footer(); ?>
|