Skip to main content

Overview

The cart page displays the items a customer has added to their cart, lets them adjust quantities or remove items, apply coupon codes, and review totals before proceeding to checkout. This guide walks through building a cart page using Lunar’s Cart model and CartSession facade. The examples below use standard Laravel controllers and Blade templates. The same concepts apply whether the storefront is built with Livewire, Inertia, or a headless API.

Fetching the Current Cart

The CartSession facade retrieves the cart for the current session. Calling current() automatically calculates totals (sub total, tax, discounts, etc.) before returning the cart.
If no cart exists in the session, current() returns null. Handle this in the controller:
Cart prices are dynamically calculated and are not stored in the database. Calling current() triggers the calculation pipeline automatically. To force a recalculation after making changes, call $cart->recalculate().

Displaying Cart Lines

Each cart line holds a reference to a Purchasable (typically a ProductVariant) along with the quantity and any custom metadata. After calculation, each line has computed properties for unit price, tax, discounts, and total.

Computed Properties on Cart Lines

After calculate() runs, each CartLine has the following properties: All values are Lunar\DataTypes\Price objects with access to value (integer), decimal() (float), and formatted() (currency string).

Accessing the Product from a Cart Line

The purchasable relationship on a cart line is polymorphic. For standard product variants:
The cart’s default eager loading (configured in config/lunar/cart.php) already loads lines.purchasable.product, lines.purchasable.values, and lines.purchasable.product.thumbnail, so these relationships are available without additional queries.

Updating Quantities

Use the updateLine() method on the cart to change a line’s quantity. This triggers validation (e.g., stock checks) and recalculates the cart.
The full method signature accepts an optional meta parameter for updating custom metadata on the line:

Validation Errors

When updating a line, Lunar runs validators defined in config/lunar/cart.php. If validation fails (for example, the requested quantity exceeds available stock), a Lunar\Exceptions\Carts\CartException is thrown.

Removing Lines

Use the remove() method to delete a line from the cart.
To remove all lines at once, use clear():

Coupon Codes

Coupon codes are stored on the cart’s coupon_code field. When the cart recalculates, the discount pipeline checks whether the code matches any active discount and applies it.

Applying a Coupon

Removing a Coupon

Displaying Applied Discounts

After calculation, the cart provides a breakdown of all applied discounts:

Cart Totals

After calculation, the cart provides all the totals needed to build a summary.

Computed Properties on the Cart

Tax Breakdown

To display a detailed tax breakdown (useful for stores with multiple tax rates):

Estimated Shipping

Shipping costs are typically calculated during checkout once a full address is provided. However, the cart page can show an estimated shipping cost based on a partial address.
Passing setOverride: true tells the cart to use the returned shipping option when calculating totals for that request. The shippingTotal on the cart will then reflect the estimate. When using CartSession, set the estimation parameters once and they persist for the session:

Routes

Putting It All Together

Here is a complete controller for the cart page:

Next Steps

  • Review the Carts reference for the full list of cart and cart line fields, configuration options, and session management.
  • Review the Extending Carts for customizing the cart calculation pipeline, adding validators, and overriding actions.
  • Review the Product Display Page guide for adding items to the cart from a product page.