Overview
Carts hold a collection of purchasable items (typically product variants) that a customer intends to order. They belong to users (which relate to customers) and support a single currency each.Cart prices are dynamically calculated and are not stored in the database. Once a cart is converted to an order, the prices are persisted on the order instead.
Cart Model
Creating a Cart
Relationships
The
draftOrder and completedOrder relationships accept an optional order ID parameter to filter to a specific order:
Scopes
Setting the Tax Zone Added in 1.5
A cart can be assigned an explicit tax zone, which lets line totals be calculated tax-inclusive before a shipping address is captured. This is typically wired up in middleware that infers the customer’s tax zone from their IP address.setTaxZone triggers a recalculation by default. Pass false as the second argument to defer the recalculation.
When
setShippingAddress is called on a cart that already has an explicit tax zone, the zone is cleared so that address-based resolution can take over. Pass clearTaxZone: false to keep the explicit zone in place.Cart Lines
Each item in a cart is represented by aCartLine. Lines link to a purchasable model (usually a ProductVariant) and track the desired quantity.
Relationships
Adding Lines
Use theadd method to add a purchasable item to the cart. If the item already exists in the cart, its quantity is updated instead.
addLines:
Updating Lines
Removing Lines
Line Validation
When adding, updating, or removing items, a series of validation actions are run (defined inconfig/lunar/cart.php). These throw a CartException on failure.
Exceptions
Lunar throws specific exceptions during cart operations to help identify what went wrong. All cart exceptions extendLunar\Exceptions\LunarException.
CartException
MessageBag containing one or more error messages.
ValidateCartForOrderCreation validator may throw a CartException with one of the following messages:
InvalidCartLineQuantityException
NonPurchasableItemException
Lunar\Base\Purchasable interface. This is checked automatically by the CartLineObserver.
CartLineIdMismatchException
DisallowMultipleCartOrdersException
createOrder() on a cart that already has a completed order, unless allowMultipleOrders: true is passed.
FingerprintMismatchException
checkFingerprint() does not match the cart’s current fingerprint. See Detecting Cart Changes for usage details.
Calculating Totals
Callcalculate() to hydrate the cart with computed prices and tax breakdowns.
recalculate:
Cart-Level Properties
Line-Level Properties
After calculation, each cart line is also hydrated with its own totals:Breakdowns
The cart also provides detailed breakdowns for tax, discounts, and shipping:Extending Cart Calculations
To programmatically change cart values (e.g. custom discounts or prices), see Cart Extending.Cart Addresses
Each cart can have a shipping and billing address, represented by theCartAddress model. These addresses are used when calculating tax breakdowns and shipping costs.
Relationships
Cached Properties
After the cart is calculated, shipping addresses are hydrated with shipping-related totals:Setting Addresses
Shipping and billing addresses can be set on the cart, which are used when calculating tax breakdowns.Address model or a CartAddress model can also be passed:
During a cart’s early lifetime, address information may not yet be available. Some countries don’t display tax until checkout. The address-based tax calculation is designed to handle this: the addresses can be set when they become available.
Shipping Options
A shipping option can be set on the cart after an address has been provided. The available options are determined by the shipping manifest.Shipping Estimates
It may be useful to show an estimated shipping cost before a full address is provided. ThegetEstimatedShipping method returns the cheapest available shipping option based on partial address data.
setOverride: true:
When
setOverride is enabled, the returned shipping option bypasses other shipping logic in the cart pipelines, but only for that single request.Creating Orders
Once a cart has been calculated and all required information (addresses, shipping, etc.) has been provided, an order can be created from the cart.allowMultipleOrders:
Checking Order Readiness
Before attempting to create an order, check whether the cart has sufficient information:Stock Validation
Stock levels can be validated before order creation. This checks each cart line against the available stock for its purchasable item.Associating Users and Customers
A user can be associated directly on the cart model. Thepolicy parameter controls how the association behaves when the user already has an existing cart: merge combines the carts, while override replaces the existing one.
Cart Session Manager
The session manager provides a convenient API for managing carts tied to the current user’s session.Configuration
Cart configuration lives inconfig/lunar/cart.php:
The default
eager_load relationships are:
config/lunar/cart_session.php:
Getting the Session Instance
Use the facade or inject the interface:current() is called, the behavior depends on the auto_create config. With auto_create set to false (default), null is returned if no cart exists, preventing unnecessary database records.
Managing Lines
Using a Specific Cart
Associating a User
Associating a Customer
A customer can be associated directly on the cart model:Forgetting the Cart
Forgetting a cart removes it from the session and soft-deletes it from the database:Shipping Estimates
When using theCartSession manager, shipping estimation parameters can be persisted so they do not need to be passed each time:
Handling User Login
When a user logs in, Lunar automatically listens to authentication events and handles cart association. If the user had a guest cart, it will be merged with (or override) any existing cart on their account, depending on theauth_policy config.
Detecting Cart Changes
Carts are dynamic: items, quantities, and prices can change at any moment. To detect whether a cart has been modified (e.g. on a different browser tab during checkout), use the fingerprint system:config/lunar/cart.php:
Pruning Old Carts
Over time, unused carts accumulate in the database. Lunar can automatically prune carts that have no associated order. Enable pruning inconfig/lunar/cart.php:
Activity Logging
The Cart model uses Spatie Activity Log to automatically record changes. All attribute changes are logged except forupdated_at.