Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/wreyesus/Sencillo_Carrito_de_Compras_en_PHP/llms.txt

Use this file to discover all available pages before exploring further.

carrito.php is the central cart page of the YUME|TEC store. On every request it reads $_SESSION['carrito'], processes any incoming POST actions (add, update quantity, or remove), writes the updated array back to the session, and then renders a cart table showing each line item alongside a running total in USD. Because all state lives in the PHP session, no database writes are needed for cart operations — the cart persists for the lifetime of the visitor’s session and is cleared only when the session is destroyed.

Session data structure

The cart is stored entirely in $_SESSION['carrito'] as a numerically-indexed PHP array of associative arrays. Each element represents one distinct product line with three keys: nombre (product name), precio (unit price as a string), and cantidad (quantity as an integer).
$_SESSION['carrito'] = [
    ['nombre' => 'Celeron Dual Core E3400', 'precio' => '53.00',  'cantidad' => 1],
    ['nombre' => 'ASUS P8Z68-V',            'precio' => '239.99', 'cantidad' => 2],
];
The array is loaded into the local variable $compras at the top of the page, mutated by whichever POST action is present, and then saved back to $_SESSION['carrito'] before the HTML is rendered.

Adding an item

Product detail pages (detalles.php) POST three fields to carrito.php: nombre, precio, and cantidad. Before appending a new element, the script scans the existing cart for a product with the same name. If a duplicate is found, its index is recorded in $duplicado and the quantities are merged rather than creating a second line:
for ($i = 0; $i <= count($compras) - 1; $i++) {
    if ($nombre == $compras[$i]['nombre']) {
        $duplicado = $i;
    }
}

if ($duplicado != -1) {
    $cantidad_nueva = $compras[$duplicado]['cantidad'] + $cantidad;
    $compras[$duplicado] = ['nombre' => $nombre, 'precio' => $precio, 'cantidad' => $cantidad_nueva];
} else {
    $compras[] = ['nombre' => $nombre, 'precio' => $precio, 'cantidad' => $cantidad];
}
$duplicado is initialised to -1 before the loop, so a value of -1 after the loop means no match was found and a fresh entry should be appended.

Updating quantity

Each cart row renders a small form containing the current quantity in a text input and a hidden id field carrying the array index of that row. When the user edits the number and presses the update image-button, cantidadactualizada and id are posted back:
if (isset($_POST['cantidadactualizada'])) {
    $id           = $_POST['id'];
    $contador_cant = $_POST['cantidadactualizada'];

    if ($contador_cant < 1) {
        $compras[$id] = NULL;
    } else {
        $compras[$id]['cantidad'] = $contador_cant;
    }
}
Setting a quantity below 1 is treated as an implicit removal: the entry is set to NULL rather than re-saving a zero quantity, which would produce a $0.00 line in the total.

Removing an item

The delete image-button in each row is inside a separate form that posts a single hidden field, id2, containing the array index of the row to delete:
if (isset($_POST['id2'])) {
    $id = $_POST['id2'];
    $compras[$id] = NULL;
}
Items are set to NULL rather than removed from the array. The rendering loop checks $compras[$i] != NULL before displaying each row, so NULL entries are silently skipped. This approach preserves the original array indices for all other rows — no reindexing occurs after a deletion.

Total calculation

After the table rows are rendered, the script accumulates a grand total by multiplying each non-NULL entry’s unit price by its quantity:
$total = 0;

for ($i = 0; $i <= count($compras) - 1; $i++) {
    if ($compras[$i] != NULL) {
        $total = $total + ($compras[$i]['cantidad'] * $compras[$i]['precio']);
    }
}
The result is displayed in the footer row of the cart table as:
$ {total} Dolares
PHP’s loose typing handles the arithmetic automatically because precio values such as "53.00" are cast to floats when used in a multiplication expression.

Proceeding to checkout

A standalone form at the bottom of the cart table contains only a submit button labelled Enviar Pedido. It posts to resumenc_compra.php with no body data:
<form name="form2" method="post" action="resumenc_compra.php">
  <input type="submit" name="button" id="button" value="Enviar Pedido">
</form>
No cart data needs to be transmitted in the POST body because $_SESSION['carrito'] already carries the full cart state. resumenc_compra.php reads the same session on the next request.

Build docs developers (and LLMs) love