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’sCart 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
TheCartSession facade retrieves the cart for the current session. Calling current() automatically calculates totals (sub total, tax, discounts, etc.) before returning the cart.
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 aPurchasable (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
Aftercalculate() 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
Thepurchasable relationship on a cart line is polymorphic. For standard product variants:
Updating Quantities
Use theupdateLine() method on the cart to change a line’s quantity. This triggers validation (e.g., stock checks) and recalculates the cart.
meta parameter for updating custom metadata on the line:
Validation Errors
When updating a line, Lunar runs validators defined inconfig/lunar/cart.php. If validation fails (for example, the requested quantity exceeds available stock), a Lunar\Exceptions\Carts\CartException is thrown.
Removing Lines
Use theremove() method to delete a line from the cart.
clear():
Coupon Codes
Coupon codes are stored on the cart’scoupon_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.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.