52 lines
3.3 KiB
PHP
52 lines
3.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
session_start();
|
|
require_once __DIR__ . '/includes/store.php';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (($_POST['action'] ?? '') === 'clear') { $_SESSION['cart'] = []; }
|
|
if (($_POST['action'] ?? '') === 'update') {
|
|
foreach (($_POST['qty'] ?? []) as $id => $qty) {
|
|
$product = get_product((string)$id);
|
|
if ($product) {
|
|
$qty = max(0, min((int)$product['stock'], (int)$qty));
|
|
if ($qty === 0) { unset($_SESSION['cart'][$id]); } else { $_SESSION['cart'][$id] = $qty; }
|
|
}
|
|
}
|
|
}
|
|
header('Location: cart.php'); exit;
|
|
}
|
|
$items = cart_items();
|
|
$addedProduct = isset($_GET['added']) ? get_product((string)$_GET['added']) : null;
|
|
layout_header('Carrito', 'Carrito de compra LA BORGUITA con precios en dólares.');
|
|
?>
|
|
<section class="py-5">
|
|
<div class="container">
|
|
<div class="section-heading"><span class="eyebrow">Carrito</span><h1>Revisa tu compra</h1><p>Ajusta cantidades antes de subir el comprobante de pago.</p></div>
|
|
<?php if ($addedProduct): ?><div class="alert alert-success alert-dismissible fade show" role="alert"><strong><?= h($addedProduct['name']) ?></strong> se agregó al carrito. Ya puedes continuar al pago o seguir comprando.<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Cerrar"></button></div><?php endif; ?>
|
|
<?php if (!$items): ?>
|
|
<div class="empty-state"><h2>Tu carrito está vacío</h2><p>Agrega productos del catálogo para iniciar un pedido.</p><a class="btn btn-dark" href="index.php#catalogo">Ir al catálogo</a></div>
|
|
<?php else: ?>
|
|
<form method="post">
|
|
<input type="hidden" name="action" value="update">
|
|
<div class="table-responsive surface"><table class="table align-middle mb-0"><thead><tr><th>Producto</th><th>Precio</th><th>Cantidad</th><th>Total</th></tr></thead><tbody>
|
|
<?php foreach ($items as $item): ?><tr><td><strong><?= h($item['name']) ?></strong><br><small class="text-secondary">Stock <?= (int)$item['stock'] ?></small></td><td><?= h(money((float)$item['price'])) ?></td><td><input class="form-control qty" type="number" name="qty[<?= h($item['id']) ?>]" value="<?= (int)$item['qty'] ?>" min="0" max="<?= (int)$item['stock'] ?>"></td><td><?= h(money((float)$item['line_total'])) ?></td></tr><?php endforeach; ?>
|
|
</tbody></table></div>
|
|
<div class="cart-actions"><button class="btn btn-outline-dark" type="submit">Actualizar cantidades</button><button class="btn btn-outline-secondary" type="submit" name="action" value="clear">Vaciar carrito</button><a class="btn btn-outline-dark" href="index.php#catalogo">Seguir comprando</a></div>
|
|
</form>
|
|
<div class="checkout-panel surface mt-3">
|
|
<div>
|
|
<span class="eyebrow">Carrito activo</span>
|
|
<h2>Listo para elegir forma de pago</h2>
|
|
<p>Tu carrito ya tiene productos. Continúa para seleccionar QR, Visa, Mastercard o transferencia.</p>
|
|
<?= payment_icons_html('mt-2') ?>
|
|
</div>
|
|
<div class="checkout-panel-actions">
|
|
<div class="total">Total: <?= h(money(cart_total())) ?></div>
|
|
<a class="btn btn-dark btn-lg" href="checkout.php">Continuar al pago</a>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</section>
|
|
<?php layout_footer(); ?>
|