Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 13, 2018 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/5a755cc088e1ca3a5b68927e33b4fa57 to your computer and use it in GitHub Desktop.
Save parzibyte/5a755cc088e1ca3a5b68927e33b4fa57 to your computer and use it in GitHub Desktop.
<?php
if(!isset($_POST["codigo"])) return;
$codigo = $_POST["codigo"];
include_once "base_de_datos.php";
$sentencia = $base_de_datos->prepare("SELECT * FROM productos WHERE codigo = ? LIMIT 1;");
$sentencia->execute([$codigo]);
$producto = $sentencia->fetch(PDO::FETCH_OBJ);
if($producto){
if($producto->existencia < 1){
header("Location: ./vender.php?status=5");
exit;
}
session_start();
$indice = false;
for ($i=0; $i < count($_SESSION["carrito"]); $i++) {
if($_SESSION["carrito"][$i]->codigo === $codigo){
$indice = $i;
break;
}
}
if($indice === FALSE){
$producto->cantidad = 1;
$producto->total = $producto->precioVenta;
array_push($_SESSION["carrito"], $producto);
}else{
$_SESSION["carrito"][$indice]->cantidad++;
$_SESSION["carrito"][$indice]->total = $_SESSION["carrito"][$indice]->cantidad * $_SESSION["carrito"][$indice]->precioVenta;
}
header("Location: ./vender.php");
}else header("Location: ./vender.php?status=4");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment