---
name: cart-state
description: "Implement a cart with explicit quantity and money invariants. Use for cart, state, localStorage."
---

# Корзина

Implement a cart with explicit quantity and money invariants.

## Implementation decisions

1. Use integer minor currency units and a currency code.
2. Key lines by product and variant; quantities must be bounded positive integers.
3. Validate persisted storage and handle unavailable localStorage. Device-local state is not server persistence.
4. Reprice and validate inventory on the server before an order.

## Working source

Read [lib/commerce.ts](../webcraft-shared/source/lib/commerce.ts) when implementing this capability. The example belongs to the WEBCRAFT React/Three.js playground except the Python commerce starter, which runs without third-party dependencies. Use [shared catalog](../webcraft-shared/source/lib/catalog.ts) only to resolve related examples. Adapt the source to the target framework and preserve existing project conventions.

## Verify observable behavior

- Repeat additions merge the same variant.
- Invalid storage cannot crash the UI.
- Removing the last line yields a usable empty state.

## Scope

Use only the parts relevant to the requested task. This skill does not grant permission to publish, spend money, change account access or expose secrets. The original brief and environment rules remain authoritative.

Live example: https://webcraft-skill-library.undrgrnd.chatgpt.site/demos/catalog

Version 2.0.0 · Original WEBCRAFT content · MIT
