Cart Module Guide
The Cart module manages shopping carts for customers, handling everything from adding items to completing checkout and placing orders.Shopping Cart
Create and manage customer shopping carts
Line Items
Add, update, and remove products from carts
Checkout Flow
Handle shipping, payment, and order creation
Promotions
Apply discounts and promotional codes
Overview
A cart is a virtual shopping bag that customers use to collect items before purchase. The Cart module provides:- Creating and managing shopping carts
- Adding, updating, and removing line items
- Managing shipping and billing addresses
- Applying promotions and calculating discounts
- Tax line calculations
- Payment collection integration
- Completing the cart to create an order
Database Schema
Cart Table
Thecart table stores the main cart entity:
Primary key with
cart prefix (e.g., cart_01H...)Associated region for pricing and tax calculations
Customer reference (null for guest carts)
Sales channel context for the cart
Customer email address
Currency for pricing (e.g., “USD”)
When the cart was completed (order placed)
Foreign key to shipping address
Foreign key to billing address
Line Item Table
Thecart_line_item table stores products in the cart:
Primary key with
cali prefixForeign key to parent cart
Product title (denormalized)
Quantity in cart
Product variant reference
Product reference
Price per unit in the cart’s currency
Whether the unit price includes tax
Whether promotions can be applied
Whether the item needs shipping
Product information is denormalized onto line items to preserve order history even if products change later.
Relationships
Core Workflows
The Cart module uses workflows for all major operations. Workflows ensure data consistency, emit events, and support compensation on failures.Create Cart Workflow
ThecreateCartWorkflow creates a new cart with optional initial items:
1
Validate inputs
Find or create the sales channel, region, and customer.
2
Get variant pricing
Retrieve prices for items being added, applying any custom pricing rules.
3
Confirm inventory
Verify sufficient stock is available for all items.
4
Create the cart
Create the cart record with line items.
5
Calculate taxes
Apply tax lines based on region and shipping address.
6
Apply promotions
Process any promotion codes provided.
7
Create payment collection
Initialize the payment collection for checkout.
Example Usage
Add to Cart Workflow
TheaddToCartWorkflow adds items to an existing cart:
1
Acquire lock
Prevent concurrent modifications to the same cart.
2
Validate cart
Ensure the cart exists and is not completed.
3
Get pricing
Retrieve prices for the variants being added.
4
Confirm inventory
Verify stock availability for the quantities requested.
5
Create or update items
If the variant already exists in cart, update quantity. Otherwise, create a new line item.
6
Refresh cart
Recalculate totals, taxes, and promotions.
7
Release lock
Allow other operations on the cart.
Complete Cart Workflow
ThecompleteCartWorkflow converts a cart into an order:
1
Validate cart
Ensure payment is authorized and shipping is configured.
2
Create order
Transform cart data into an order record.
3
Reserve inventory
Decrement available stock for ordered items.
4
Register promotion usage
Track that promotions were used for reporting.
5
Link order to cart
Create the order-cart relationship.
6
Authorize payment
Complete payment authorization with the provider.
7
Emit order.placed event
Trigger downstream workflows (notifications, fulfillment, etc.).
Calculated Totals
Request total fields when you need them. The module calculates these on-demand:| Field | Description |
|---|---|
total | Grand total including all taxes and shipping |
subtotal | Items total before tax and discounts |
tax_total | Total tax amount |
discount_total | Total discount amount |
shipping_total | Shipping cost with tax |
item_total | Items subtotal with tax |
item_subtotal | Items subtotal without tax |
Requesting Totals
When you request total fields, the module automatically includes required relations for calculations.
Integration with Other Modules
Product Module
Line items reference products and variants:Payment Module
Carts have a payment collection for checkout:- Payment collection is created automatically on cart creation
- Customers select a payment provider and authorize payment
- Payment is finalized during cart completion
Promotion Module
Promotions are applied through adjustments:- Line item adjustments for product discounts
- Shipping method adjustments for shipping discounts
- Promotion usage is tracked when cart is completed
Tax Module
Tax lines are calculated based on:- Region tax rates
- Shipping address
- Product tax classes
Inventory Module
Inventory is managed at two points:- Add to Cart - Confirm inventory is available
- Complete Cart - Reserve inventory for the order
Order Module
When a cart is completed:- Order is created from cart data
- Link is established between order and cart
- Cart is marked as completed
Order-Cart Link
Theorder_cart link module creates a relationship between orders and carts:
- Query the original cart from an order
- Find which order was created from a cart
- Maintain audit trail for order history
Complete Checkout Example
Best Practices
Use workflows for cart operations
Use workflows for cart operations
Always use the provided workflows instead of direct service calls. Workflows handle locking, event emission, and compensation on failures.
Request totals only when needed
Request totals only when needed
Total calculations require loading additional relations. Only include total fields when displaying to users.
Handle concurrent modifications
Handle concurrent modifications
The cart workflows use locking to prevent race conditions. If you build custom operations, implement similar locking.
Check completed_at before modifications
Check completed_at before modifications
Always verify a cart is not completed before attempting modifications. Completed carts cannot be changed.
Denormalize product data
Denormalize product data
Line items store product info to preserve order history. Don’t rely on product lookups for historical data.