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).
$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:
$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 hiddenid 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:
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:
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:
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 toresumenc_compra.php with no body data:
$_SESSION['carrito'] already carries the full cart state. resumenc_compra.php reads the same session on the next request.