40 lines
2.2 KiB
PHP
40 lines
2.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
session_start();
|
|
require_once __DIR__ . '/includes/store.php';
|
|
$product = get_product((string)($_GET['id'] ?? ''));
|
|
if (!$product) { http_response_code(404); layout_header('Producto no encontrado'); echo '<section class="py-5"><div class="container"><div class="alert alert-warning">Producto no encontrado.</div><a href="index.php" class="btn btn-dark">Volver</a></div></section>'; layout_footer(); exit; }
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$qty = max(1, min((int)$product['stock'], (int)($_POST['qty'] ?? 1)));
|
|
$_SESSION['cart'][$product['id']] = min((int)$product['stock'], (int)($_SESSION['cart'][$product['id']] ?? 0) + $qty);
|
|
if (($_POST['next'] ?? 'cart') === 'checkout') {
|
|
header('Location: checkout.php');
|
|
exit;
|
|
}
|
|
header('Location: cart.php?added=' . rawurlencode((string)$product['id']));
|
|
exit;
|
|
}
|
|
layout_header($product['name'], $product['description']);
|
|
?>
|
|
<section class="py-5">
|
|
<div class="container">
|
|
<a href="index.php#catalogo" class="back-link">← Volver al catálogo</a>
|
|
<div class="row g-4 align-items-center mt-2">
|
|
<div class="col-lg-5"><div class="product-visual detail"><span><?= h(substr($product['name'], 0, 2)) ?></span></div></div>
|
|
<div class="col-lg-7">
|
|
<span class="badge subtle"><?= h($product['category']) ?></span>
|
|
<h1 class="fw-bold mt-3"><?= h($product['name']) ?></h1>
|
|
<p class="lead text-secondary"><?= h($product['description']) ?></p>
|
|
<div class="price big"><?= h(money((float)$product['price'])) ?></div>
|
|
<p class="stock mt-2">Disponible en almacén: <?= (int)$product['stock'] ?> unidades</p>
|
|
<form method="post" class="d-flex gap-2 mt-4 checkout-strip flex-wrap">
|
|
<input class="form-control" type="number" name="qty" value="1" min="1" max="<?= (int)$product['stock'] ?>" aria-label="Cantidad">
|
|
<button class="btn btn-dark btn-lg" type="submit" name="next" value="cart">Agregar al carrito</button>
|
|
<button class="btn btn-outline-dark btn-lg" type="submit" name="next" value="checkout">Pagar ahora</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<?php layout_footer(); ?>
|