Hola!
Soy algo nueva en la programación de PHP y estoy intentando hacer una página de agregar productos a carrito de compras. Ya he logrado buscar el producto y agregarlo a mi carrito pero no he podido lograr modificar la cantidad, cuando se agrega se agrega solamente 1.
Esta es mi tabla de resultados cuando busco los productos:
Esta es mi tabla de carrito de compras cuando le doy click al botón de agregar a carrito:
El archivo que llama al PHP de agregar al carrito es llamado agregar-producto-carrito.php que es el siguiente:
Código PHP:
Ver original<?php
if(isset($_SESSION['User'])&&(isset($_GET['ProductoId']))) { $_SESSION['Cart'][] = array( 'ProductoId' => (int) $_GET['ProductoId'],
'Cantidad' => 1
);
}
header('Location: http://localhost:8888/carrito-compras.php');
Mi pregunta es, en la tabla de resultados, ¿tengo que agregar una columna nueva llamada "Cantidad" con un opción de select o textfield? O en la tabla del carrito de compras agregar algo ahí.
Este es mi código php de la tabla de resultados:
Código PHP:
Ver original<?php
if (isset($_POST['TipoId'])) { $id = $_POST['TipoId'];
} else {
$id = "";
}
$query = "SELECT Descripcion, Precio, Marca, ProductoId FROM productos WHERE TipoId = '" .$id."'";
$numero = 0;
?>
<div id="resultados"><p class="titulo5">Resultados</p>
<table class="table" width="900" border="1">
<thead>
<tr>
<th width="532" align="center" height="34">Producto/Capacidad</th>
<th width="126">Marca</th>
<th width="81">Precio</th>
<th width="133">Link</th>
<th width="126">Agregar a Mi Orden</th>
</tr>
</thead>
<tbody>
<?php
{
printf("<td width='38%%'><font face='verdana'>%s</font></td>", $row['Descripcion']); printf("<td width='10%%'><font face='verdana'>%s</font></td>", $row['Marca']); printf("<td width='7%%'><font face='verdana'>%s</font></td>", $row['Precio']); printf("<td width='10%%'><font face='verdana'><a href='/pdf/%s.pdf'>Descargar Datasheet</a></font></td>", $row['ProductoId']); printf("<td width='10%%'><font face='verdana'><a href='agregar-producto-carrito.php?ProductoId=%s'>Agregar a la orden</a></font></td></tr>", $row['ProductoId']); $numero++;
}
?>
</tbody>
</table>
Y este es el código de la tabla de carrito de compras:
Código PHP:
Ver original<?php
if (isset($_SESSION['Cart']) ) { ?>
<div id="resultados"><p class="titulo5">Carrito de Compra</p>
<div id="myDiv">
<table class="table" width="900" border="1">
<thead>
<tr>
<th width="532" height="34">Producto</th>
<th width="81">Marca</th>
<th width="133">Cantidad</th>
<th width="126">Precio</th>
<th width="126">Modificar</th>
</tr>
</thead>
<tbody>
<?php
$preciototal=0;
foreach ($_SESSION['Cart'] as $item)
{
$query = "SELECT * FROM vista_productos WHERE ProductoId = '" .$item['ProductoId']."'";
echo "<tr><td width=\"40%\"><font face=\"verdana\">".
$row['Descripcion']. "</font></td>";
echo "<td width=\"10%\"><font face=\"verdana\">" .
$row['Marca']. "</font></td>";
echo "<td width=\"5%\"><font face=\"verdana\">" .
$item['Cantidad']. "</font></td>";
echo "<td width=\"10%\"><font face=\"verdana\">".
$item['Cantidad']*$row['Precio']."</font></td>";
printf("<td width='10%%'><font face='verdana'><a href='eliminar-producto-carrito.php?ProductoId=%s'>Eliminar</a></font></td></tr>", $row['ProductoId']); $numero++;
$preciototal+=$item['Cantidad']*$row['Precio'];
}
?>
</tbody>
</table>
<h2> Precio Total: $<?php echo $preciototal;
?> </h2>
Espero me puedan ayudar ya que llevo ya tiempo intentando resolver esto y no he podido.
Muchas gracias!