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(); }