The cart system maintains one active cart per user. The first time a product is added, a new cart document is created and linked to the authenticated user. Every subsequent add or quantity update modifies that same cart — there is no need to create a cart explicitly. Subtotals and item counts are recalculated automatically on every mutation, and stock levels (Documentation Index
Fetch the complete documentation index at: https://mintlify.com/fredy-rizo/Ecommerce/llms.txt
Use this file to discover all available pages before exploring further.
minCant) are decremented when items are added and restored when they are removed or decreased.
Add a product to the cart
When a product is added, the item’s
minCant (remaining stock) is decremented by cantBuy. If you later decrease the quantity or the item is removed, that stock is restored automatically.POST /api/carts/agregate/:productId/cart
Adds a product to the authenticated user’s cart. If the product–size combination already exists in the cart, the existing line’s subtotal is incremented rather than creating a duplicate entry. If the user has no existing cart, one is created on the fly.
Path parameters
The unique identifier of the product to add to the cart.
Headers
A valid JWT token obtained from the authentication endpoint. Format:
<jwt-token>Request body
The quantity of the product to add. Must be greater than
0. A 203 is returned if this value is <= 0. The stock check (cantBuy > product.minCant) is only evaluated when product.minCant is truthy — if minCant is 0 the falsy short-circuit bypasses the check and the decrement proceeds.The selected size(s) for the product. Used together with the product ID to identify duplicate cart entries — adding the same product with the same sizes (compared via
JSON.stringify) increments the existing cart line’s subtotal rather than creating a new one.Pricing logic
When the product has adiscountPrice set (truthy), that value is used as the unit price; otherwise the standard price field is used. Both are parsed with parseFloat. The per-line subtotal is calculated as:
subtotal is always the sum of all per-line subtotals.
Cart creation vs update logic
Validate request
cantBuy must be > 0. The product must exist. If product.minCant is truthy and cantBuy > product.minCant, a 203 is returned with "Stock no disponible". A 404 is returned if the product does not exist.Resolve unit price
If
discountPrice is truthy on the product, it is used; otherwise price is used. Both are parsed as floats.Find or create cart
The user’s existing cart is located with
Carts.findOne({"user._id": req.user._id}). If none exists, a new cart document is created with cantBuy, subtotal, products, and user set directly from the request.Deduplicate by product + size (existing cart only)
For an existing cart, the products array is searched for an entry where
p._id === productX._id.toString() && JSON.stringify(p.sizes) === JSON.stringify(sizes). If a match is found, only the matching line’s subtotal is increased by subtotalProducto. If no match is found, the product is pushed as a new line item (without a cantBuy field on the line). Then cart.cantBuy is incremented by the request’s cantBuy and cart.subtotal is recalculated as the sum of all line subtotals.Responses
"Producto agregado/actualizado al carrito" on success.true on success.The updated total monetary value of the cart after the operation.
The updated total number of individual items across all cart lines.
The full, updated cart document.
Error responses
| Status | Meaning |
|---|---|
203 | cantBuy is <= 0 (invalid quantity). |
203 | product.minCant is truthy and cantBuy > product.minCant (insufficient stock). |
404 | Product not found. |
Example
Update item quantity in cart
POST /api/carts/:productId/cart/:cartId
Increments or decrements the quantity of a specific product in an existing cart. Ownership is validated — the cart is looked up by both _id and user._id, so a user cannot modify another user’s cart. Stock is managed in both directions: increasing consumes stock, decreasing restores it.
Path parameters
The ID of the product line to modify within the cart.
The ID of the cart document to update.
Headers
A valid JWT token. Format:
<jwt-token>Request body
The quantity change direction. Accepted values:
"increase"— adds 1 to the line’scantBuy, deducts 1 fromproduct.minCant. Fails with203ifproduct.minCant <= 0."decrease"— subtracts 1 from the line’scantBuy, restores 1 toproduct.minCant. IfcantBuyis already<= 1, the entire product line is removed from the cart and the fullcantBuyquantity is restored toproduct.minCant.
Increase behavior
- Returns
203with"Sin stock disponible"ifproduct.minCant <= 0. cartProduct.cantBuyis incremented by 1.cartProduct.subtotalis recalculated asprice × cantBuy.product.minCantis decremented by 1 and saved.- Cart totals are recalculated from all lines.
Decrease behavior
- If
cartProduct.cantBuy <= 1: the fullcantBuyquantity is restored toproduct.minCant, the product line is removed from theproductsarray viasplice, and cart totals are recalculated. - Otherwise:
cartProduct.cantBuyis decremented by 1,cartProduct.subtotalis recalculated,product.minCantis restored by 1, and cart totals are recalculated.
Responses
"Carrito actualizado" on success.true on success.Updated total cart value.
Updated total item count.
Full updated cart document (same shape as described in the add-to-cart section above).
Error responses
| Status | Meaning |
|---|---|
400 | action is not "increase" or "decrease". |
404 | Cart not found or does not belong to the authenticated user. |
404 | Product not found in the cart, or product not found in the database. |
203 | Attempted to increase but product.minCant <= 0 (no stock remaining). |