Skip to main content
The cart system is extended by swapping action contracts in the container, and by registering pipeline steps and validators for its calculation and validation lists.

Overview

Every operation Lunar\Core\Models\Cart performs — adding a line, setting a shipping option, creating an order — is a one-line delegation from a verb method on the model to an action contract resolved from the container. Swapping behavior means binding a different implementation of that contract in a service provider; the verb picks it up automatically.

Cart action contracts

Lunar\Core\ActionServiceProvider’s $actions map is the canonical list of swappable action seams. The cart-related entries, each a Lunar\Core\Contracts\Actions\Carts\* interface bound to its Lunar\Core\Actions\Carts\* default implementation: Rebind a contract from a consumer’s own service provider:
Config-string class substitution (config('lunar.*', SomeClass::class)) is not used for these seams — the contract is bound in the container, and a consumer swaps it there. Do not add a config key to point at a custom class.

Calculation pipelines

Cart totals are calculated by running the cart through an ordered list of pipeline classes, and cart lines through a second list. Both lists live in config/lunar/cart.php, as ordered lists rather than single-implementation swaps:
Add a pipe by appending a class to the relevant array. A cart pipe implements handle(Cart $cart, Closure $next): mixed; a cart line pipe implements the same shape against CartLine.
Pipes run in the order listed, top to bottom.

Validators

Cart::add(), updateLine(), remove(), addAddress(), setShippingOption(), and createOrder() each run a list of validators, configured the same way, in config/lunar/cart.php:
A validator extends Lunar\Core\Validation\BaseValidator:
Register it against the relevant action in config/lunar/cart.php:
If validation fails, Lunar\Core\Exceptions\Carts\CartException is thrown. Errors are accessed the same way as Laravel’s own validation responses:

Cart line modifiers

A cart line unit price and subtotal can be adjusted just after it is calculated, using a Lunar\Core\Modifiers\CartLineModifier:
CartLineModifier also exposes calculating() and calculated() hooks. Register an instance with the Lunar\Core\Modifiers\CartLineModifiers collection, resolved from the container:
Lunar\Core\Modifiers\CartModifier and its CartModifiers collection are also registered in the container, but nothing in core currently runs a cart through them — only CartLineModifier (via CalculateLineSubtotal) and the shipping equivalent, ShippingModifier (see Extending Shipping), are wired into a live calculation step. Do not rely on CartModifier for cart-level hooks until it is dispatched somewhere in core.

Overriding order creation

Cart::createOrder() delegates to Contracts\Carts\CreatesOrder, which in turn runs the order through the pipeline configured in config/lunar/orders.php. See Extending Orders for the order-side pipeline and action contracts.