0% found this document useful (0 votes)
11 views

2

This PHP code handles adding products to a shopping cart session. When a product ID is submitted, it checks if the product is already in the cart. If not, it is added with details like name, price. If it is, a warning message is shown. The total items in the cart is counted and products are displayed with an "Add" button to submit the form and add it to the cart.

Uploaded by

N R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

2

This PHP code handles adding products to a shopping cart session. When a product ID is submitted, it checks if the product is already in the cart. If not, it is added with details like name, price. If it is, a warning message is shown. The total items in the cart is counted and products are displayed with an "Add" button to submit the form and add it to the cart.

Uploaded by

N R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?

php
session_start();
include './controller/funciones.php';
$con = conexion();
$status = "";
$conteo = 0;
// session_unset();
// session_destroy();

if(isset($_POST['id']) && $_POST['id']!= ""){


$id = $_POST['id'];
$stmt = $con -> prepare("SELECT * FROM productos WHERE id=?;");
$stmt-> execute([$id]);
$pr = $stmt -> fetch(PDO::FETCH_ASSOC);
$nombre = $pr['nombre'];
$id = $pr['id'];
$precio = $pr['preciov'];
$img = $pr['img'];

$orden = array(
$id => array(
'nombre' => $nombre,
'id' => $id,
'precio' => $precio,
'cantidad' => 1,
'img' => $img,
)
);

// Si la sesion no ha sido creada


if(empty($_SESSION['orden'])){
$_SESSION['orden'] = $orden;
$status = "El producto ha sido agregado al carrito";
echo '<div class="m-0 alert alert-primary alert-dismissible fade show">
<button class="btn-close" type="button" data-bs-
dismiss="alert"></button>
'.$status.'
</div>';
}else{
$articulos_arreglo = array_keys($_SESSION['orden']);

if(in_array($id, $articulos_arreglo)){
$status = "El producto ya esta en el carrito";
echo '<div class="m-0 alert alert-warning alert-dismissible fade
show">
<button class="btn-close" type="button" data-bs-
dismiss="alert"></button>
'.$status.'
</div>';
}else{
// array_merge - para fusionar arreglos
$_SESSION['orden'] = array_merge(
$_SESSION['orden'], $orden
);

$status = "El producto ha sido añadido al carrito";


echo '<div class="m-0 alert alert-info alert-dismissible fade
show">
<button class="btn-close" type="button" data-bs-
dismiss="alert"></button>
'.$status.'
</div>';
}
}
}

if(!empty($_SESSION['orden'])){
$conteo = count(array_keys($_SESSION['orden']));
}

$stmt = $con -> prepare("SELECT * FROM productos;");


$stmt-> execute();
$pr = $stmt -> fetchAll(PDO::FETCH_ASSOC);
?>

<?= plantillaHeader("Articulo", $conteo); ?>


<div class="row productos p-5 pt-0 mt-5">
<?php foreach($pr as $productos): ?>

<form action="" method="POST">


<input type="hidden" name="id" id="id" value="<?php echo
$productos['id']; ?>">
<div class="col-lg-12 pb-4">
<div class="card h-100 bg-primary bg-opacity-25" style="border:
transparent;">
<div class="row">
<div class="col-lg-3">
<img src="<?php echo $productos['img'] ?> "
class="card-img-top h-100 img-fluid p-2" alt="<?php echo
$productos['descripcion'] ?> ">
</div>

<div class="col-lg-9 p-4">


<div class="card-body p-3">
<p class="card-title h2"> <?php echo
$productos['nombre'] ?> </p>
<p class="card-text"> <?php echo
$productos['descripcion'] ?> </p>
<p class="card-text"> <b> Precio: </b> $<?php echo
$productos['preciov'] ?>
|| <b> Cantidad: </b> <?php echo
$productos['cantidad'] ?> </p>
</div>

<div class="card-footer pb-4" style="background-color:


transparent; border: transparent;">
<div class="btn-group container-fluid">
<button class="btn fw-bold btn-outline-primary
bi bi-cart-plus-fill" type="submit"> Añadir </button>
<a class="btn fw-bold btn-outline-primary bi
bi-eye-fill" type="button" href="index.php?pagina=producto&id=<?php echo
$productos['id'] ?>" class="producto"> Ver Producto </a>
</div>
</div>
</div>
</div>
</div>
</div>
</form>

<?php endforeach; ?>


</div>

<?=plantillaFooter() ?>

You might also like