Carts & Checkout
Create carts, add items, apply discounts, convert to orders
Carts & Checkout
Flow
Create cart → Add items → Update quantity → Apply discount code → Create orderCreate Cart
const cart = await client.from('carts').create({
customer: customerId, // omit for guest
status: 'active'
})Statuses: active (default) → completed (converted to order) / abandoned (expired)
Add Items
await client.from('cart-items').create({
cart: cartId,
product: productId,
variant: variantId,
option: optionId,
quantity: 2,
unitPrice: 39000
})View Cart
// Cart items
const { docs: items } = await client.from('cart-items').find({
where: { cart: { equals: cartId } },
depth: 1 // includes product, variant, option info
})
// Cart totals
const cart = await client.from('carts').findById(cartId)
// cart.itemsTotal → items subtotal (auto-calculated)
// cart.shippingFee → shipping fee
// cart.discountAmount → discount amount
// cart.totalAmount → final amount (auto-calculated)Update Quantity & Remove
// Update quantity
await client.from('cart-items').update(itemId, {
quantity: 3
})
// Remove item
await client.from('cart-items').delete(itemId)Apply Discount Code
// 1. Validate code
const { docs } = await client.from('discounts').find({
where: {
code: { equals: 'WELCOME10' },
isActive: { equals: true },
endsAt: { greater_than: new Date().toISOString() }
}
})
if (docs.length === 0) {
// Invalid code
}
// 2. Apply to cart
await client.from('carts').update(cartId, {
discountCode: 'WELCOME10'
})
// 3. Check discounted amount
const updated = await client.from('carts').findById(cartId)
// updated.discountAmount → discount amount
// updated.totalAmount → final amount after discountDiscount types: percentage, fixed_amount, free_shipping
Convert to Order
// 1. Complete cart
await client.from('carts').update(cartId, {
status: 'completed'
})
// 2. Create order from cart
const order = await client.from('orders').create({
customer: customerId,
cart: cartId,
shippingAddress: {
recipientName: 'John Doe',
phone: '010-1234-5678',
postalCode: '06123',
address1: '123 Main St',
address2: 'Apt 4B'
}
})Collection Reference
For detailed field specifications, see Cart Collections and Discount Collections.