Skip to Content

Cart

Hoikka uses cookie-based cart tokens for guest-friendly shopping. Carts are orders in "created" state, managed through orderService.

Cart Tokens

+page.server.ts
export const load = async ({ cookies }) => { const token = cookies.get("cart_token") ?? crypto.randomUUID(); cookies.set("cart_token", token, { path: "/", maxAge: 60 * 60 * 24 * 30 // 30 days }); const cart = await orderService.getOrCreateActiveCart({ cartToken: token }); return { cart }; };

Cart Operations

Add Item

await orderService.addLine(cartId, { variantId, quantity }); // If item exists, quantity is increased

Update Quantity

await orderService.updateLineQuantity(cartId, lineId, newQuantity);

Remove Item

await orderService.removeLine(cartId, lineId);

Cart Structure

interface Cart { id: number; token: string; lines: CartLine[]; subtotal: number; createdAt: Date; } interface CartLine { id: number; variantId: number; variant: ProductVariant; quantity: number; unitPrice: number; total: number; }

Stock Validation

Stock is validated when adding items. Stock reservations are created with a 15-minute expiry. See ARCHITECTURE.md for the full reservation lifecycle.

Last updated on