catalogo y carrito de compras
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
include("db.php");
|
||||
|
||||
if (!isset($_SESSION['id_usuario'])) {
|
||||
header("Location: login.php?msj=debes_iniciar_sesion");
|
||||
exit();
|
||||
}
|
||||
|
||||
$accion = $_GET['accion'] ?? '';
|
||||
$id = $_GET['id'] ?? '';
|
||||
$id = intval($id);
|
||||
|
||||
$origen = $_GET['origen'] ?? 'menu';
|
||||
$cat_retorno = $_GET['cat'] ?? 'Todos';
|
||||
|
||||
switch ($accion) {
|
||||
case 'agregar':
|
||||
if ($id > 0) {
|
||||
$sql_stock = "SELECT stock FROM productos WHERE id = ?";
|
||||
$stmt = $conexion->prepare($sql_stock);
|
||||
$stmt->bind_param("i", $id);
|
||||
$stmt->execute();
|
||||
$resultado = $stmt->get_result();
|
||||
|
||||
if ($row = $resultado->fetch_assoc()) {
|
||||
$stock_disponible = (int)$row['stock'];
|
||||
|
||||
|
||||
$cantidad_futura = isset($_SESSION['carrito'][$id]) ? $_SESSION['carrito'][$id] + 1 : 1;
|
||||
|
||||
if ($cantidad_futura <= $stock_disponible) {
|
||||
$_SESSION['carrito'][$id] = $cantidad_futura;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($origen === 'carrito') {
|
||||
header("Location: carrito.php");
|
||||
} else {
|
||||
header("Location: productos.php?cat=" . urlencode($cat_retorno) . "#prod-" . $id);
|
||||
}
|
||||
exit();
|
||||
|
||||
case 'quitar':
|
||||
if (isset($_SESSION['carrito'][$id])) {
|
||||
$_SESSION['carrito'][$id]--;
|
||||
if ($_SESSION['carrito'][$id] <= 0) {
|
||||
unset($_SESSION['carrito'][$id]);
|
||||
}
|
||||
}
|
||||
header("Location: carrito.php");
|
||||
exit();
|
||||
|
||||
case 'eliminar':
|
||||
unset($_SESSION['carrito'][$id]);
|
||||
header("Location: carrito.php");
|
||||
exit();
|
||||
|
||||
default:
|
||||
header("Location: productos.php");
|
||||
exit();
|
||||
}
|
||||
Reference in New Issue
Block a user