Skip to main content

Get Cart Items (Customer)

Retrieves all items in the customer’s shopping cart.

Path Parameters

customerId
string (uuid)
required
Customer identifier (must match authenticated user)

Response

items
array
Array of cart item objects
totalPrice
number
Total price of all items in cart
itemCount
integer
Total number of items in cart

Response Codes

  • 200 OK - Cart items retrieved
  • 404 Not Found - Customer not found

Examples

curl -X GET "https://your-server.com/api/customers/cart-items/customer-uuid" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Success Response

{
  "items": [
    {
      "id": "cart-item-1",
      "productId": "product-uuid",
      "productName": "Premium Wireless Headphones",
      "productImage": "https://cdn.example.com/products/headphones.jpg",
      "price": 249.99,
      "discountPrice": 199.99,
      "quantity": 1,
      "totalPrice": 199.99,
      "variantId": null,
      "sellerId": "seller-uuid",
      "sellerName": "AudioPro Shop",
      "inStock": true,
      "availableQuantity": 15
    },
    {
      "id": "cart-item-2",
      "productId": "product-uuid-2",
      "productName": "USB-C Cable",
      "productImage": "https://cdn.example.com/products/cable.jpg",
      "price": 19.99,
      "discountPrice": null,
      "quantity": 2,
      "totalPrice": 39.98,
      "variantId": "variant-uuid",
      "variantName": "6ft - Black",
      "sellerId": "seller-uuid-2",
      "sellerName": "TechGear",
      "inStock": true,
      "availableQuantity": 50
    }
  ],
  "totalPrice": 239.97,
  "itemCount": 2
}

Add Product to Cart (Customer)

Adds a product to the shopping cart.

Request Body

customerId
string (uuid)
required
Customer identifier
productId
string (uuid)
required
Product to add to cart
quantity
integer
required
Quantity to add (must be > 0)
variantId
string (uuid)
Product variant ID (if product has variants)

Response Codes

  • 204 No Content - Product added to cart
  • 404 Not Found - Product, customer, or variant not found

Examples

curl -X POST "https://your-server.com/api/customers/cart-items" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "customerId": "customer-uuid",
    "productId": "product-uuid",
    "quantity": 1,
    "variantId": null
  }'

Remove Item from Cart (Customer)

Removes an item from the shopping cart.

Path Parameters

customerId
string (uuid)
required
Customer identifier
cartItemId
string (uuid)
required
Cart item identifier to remove

Response Codes

  • 204 No Content - Item removed from cart
  • 404 Not Found - Customer or cart item not found

Examples

curl -X DELETE "https://your-server.com/api/customers/cart-items/customer-uuid/cart-item-uuid" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Cart Management Tips

Cart Behavior

Duplicate Items: Adding the same product increases the quantity of the existing cart item rather than creating a duplicate.
Persistent Cart: Cart items are stored in the database and persist across sessions.
Stock Availability: Check stock availability before checkout. Items may sell out between adding to cart and completing purchase.

Complete Cart Flow Example

// 1. Add item to cart
async function addToCart(productId, quantity = 1, variantId = null) {
  const response = await fetch('/api/customers/cart-items', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      customerId,
      productId,
      quantity,
      variantId
    })
  });
  
  if (response.status === 204) {
    // Refresh cart display
    await loadCart();
  }
}

// 2. Load cart items
async function loadCart() {
  const response = await fetch(
    `/api/customers/cart-items/${customerId}`,
    {
      headers: { 'Authorization': `Bearer ${token}` }
    }
  );
  
  const cart = await response.json();
  displayCart(cart);
  updateCartBadge(cart.itemCount);
}

// 3. Remove item
async function removeFromCart(cartItemId) {
  const response = await fetch(
    `/api/customers/cart-items/${customerId}/${cartItemId}`,
    {
      method: 'DELETE',
      headers: { 'Authorization': `Bearer ${token}` }
    }
  );
  
  if (response.status === 204) {
    await loadCart();
  }
}

// 4. Proceed to checkout
async function checkout() {
  const cart = await loadCart();
  
  // Check stock availability
  const outOfStock = cart.items.filter(item => !item.inStock);
  if (outOfStock.length > 0) {
    alert('Some items are out of stock');
    return;
  }
  
  // Proceed to delivery selection and payment
  window.location.href = '/checkout';
}

Updating Quantities

To update the quantity of an item in the cart:
  1. Remove the existing cart item
  2. Add the product again with the new quantity
Or implement quantity update UI:
async function updateQuantity(cartItemId, newQuantity) {
  if (newQuantity <= 0) {
    await removeFromCart(cartItemId);
  } else {
    // Get product details from cart item
    const cart = await loadCart();
    const item = cart.items.find(i => i.id === cartItemId);
    
    // Remove old
    await removeFromCart(cartItemId);
    
    // Add with new quantity
    await addToCart(item.productId, newQuantity, item.variantId);
  }
}

Cart Total Calculation

function calculateCartTotal(cart) {
  // Calculate subtotal (with discounts applied)
  const subtotal = cart.items.reduce((sum, item) => {
    const price = item.discountPrice || item.price;
    return sum + (price * item.quantity);
  }, 0);
  
  // You can add delivery cost here if known
  // const total = subtotal + deliveryCost;
  
  return subtotal;
}

Build docs developers (and LLMs) love