Cart invariants
These rules apply whether the UI is custom or Hosted Checkout:- Every cart belongs to an anonymous or logged-in session.
- Commerce Engine does not create an empty cart.
- The first item creates the cart.
- Later item changes mutate the existing cart.
variant_idis always present in item operations; usenullfor a simple product.- A mutation returns the full updated cart.
- Login merges the active anonymous cart into the account session.
to_be_paidis the final amount to show after loyalty and credit deductions.- Carts can expire and must be recovered gracefully.
Hosted Checkout cart
@commercengine/storefront, use provided auth mode and two-way token synchronization. See the framework-specific Hosted Checkout guide.
Custom cart setup
Use the session accessor:Retrieve the active cart
First Add to Cart
Create the cart with at least one item:variantId must be the resolved purchasable variant ID. For a simple product it must be null.
Creating the cart on first add
Add, update, and remove items
Use the same operation for subsequent item changes.quantity is the desired total quantity, not a delta.
Serialize cart mutations
Rapid clicks or quantity changes can start multiple mutations concurrently. Because every response contains a complete cart snapshot, an older response can overwrite a newer state. Queue cart mutations with concurrency1:
Serialized cart mutations
Optimistic UI
Optimistic updates can improve perceived speed, but they do not remove the need to serialize server mutations. A safe pattern:- calculate and render an optimistic local state
- enqueue the server operation
- replace local state with the returned cart
- roll back or refetch on failure
- announce the result for assistive technology
to_be_paid solely on the client.
Persist and recover cart state
The active cart is tied to the session, sogetUserCart() is the preferred recovery path. A custom cart can also persist the last cart_id to reduce lookup ambiguity.
On startup or cart open:
- read the persisted cart ID if present
- fetch that cart
- check
expires_at - if missing/expired, clear the stored ID
- fall back to
getUserCart() - create a new cart only when the next item is added
Restoring the active cart
Login and logout transitions
Login
Commerce Engine merges the active anonymous cart into the logged-in session. After verification:- invalidate/refetch the cart query
- do not copy items manually
- do not create another cart
Logout
Logout returns a new anonymous token pair. Preserve those tokens through managed SDK storage, then refetch cart state. Do not callclearTokens() after a successful logout.
Cart structure
Important fields include:
Render money using the currency information returned by the cart; do not assume one currency symbol.
Coupons and promotions
Automatic promotions are reflected in the returned cart. For a custom coupon UI:Loyalty and credit balance
Loyalty and credit operations require the appropriate logged-in customer state. After every redemption/removal:- use the returned cart
- update
to_be_paid - handle the zero-payment case during order creation
- do not subtract values twice in the UI
Addresses and fulfilment
Addresses, serviceability, and fulfilment selection change cart totals. Keep them in the custom checkout flow rather than treating the cart shown on the PLP/PDP as final. See Checkout for:- saved address linking
- pincode lookup and deliverability
- multiple shipments
- delivery versus collect-in-store
- payment and retry
Cart deletion
Use the exact SDK method contract for deleting a cart and confirm its response semantics. After successful deletion:- clear persisted cart ID
- set local cart state to empty
- invalidate cart badge/query state
Error handling
Treat errors by operation and status:
Do not retry cart mutations blindly because the first attempt may have succeeded server-side.
Accessibility
- cart count should use
aria-live="polite" - quantity controls need accessible names including the product
- disable controls while their queued mutation is active
- return focus appropriately after removing an item
- announce errors and successful updates
- maintain keyboard focus when a drawer opens/closes
Production checklist
Cart skills and starter patterns
Use the cart/checkout skill for exact edge cases and the production starters for integrated query, session, and Hosted Checkout patterns.