Overview
Every operationLunar\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.Registering calculation steps
Cart calculation, cart line pricing, price matching, order creation, the cart validators and cart pruning each run an ordered list of classes, called a step list. Each list is named by a case of theLunar\Core\Enums\StepList enum, seeded by core in code, and changed from a service provider through the Lunar\Core\Facades\Steps facade:
Steps::for() returns the list’s Lunar\Core\Manifests\StepSequence:
Each method except
all() returns the sequence, so calls chain:
- Adding a class that is already in the list does nothing, so a service provider that boots twice cannot run a step twice.
- Changes are recorded in registration order and applied when the list is first read. Register them in
register()orboot(); changing a list during a request is unsupported. before(),after()orreplace()with an anchor that is not in the list throwsLunar\Core\Exceptions\UnknownStepExceptionwhen the list is read, naming both classes. A step positioned against one that another service provider removed fails loudly rather than running in the wrong place.
The step lists
List what runs, including steps registered by add-ons, with:
Writing a cart step
ACartCalculation step implements handle(Cart $cart, Closure $next): mixed. A CartLinePricing step has the same shape against CartLine.
Totals caching
Cart::calculate() delegates to Lunar\Core\Contracts\Actions\Carts\CalculatesCart. The default Lunar\Core\Actions\Carts\CalculateCart chooses between the totals already in memory, the snapshot persisted on the carts row, and a run of the modifiers and the CartCalculation steps (see Persisted totals). Steps stay free of persistence: CalculateCart writes the snapshot through PersistsCartTotals after the run, and reads it back through HydratesCartTotals.
To change the caching policy, bind a different CalculatesCart in a service provider. Extending CalculateCart keeps the calculation itself. This one never serves the snapshot to a cart with a coupon applied:
lunar.cart.totals.ttl to 0 instead. No binding is needed.
Validators
Cart::add(), updateLine(), remove(), addAddress(), setShippingOption(), and createOrder() each run the validators in their step list (see the table above). A validator extends Lunar\Core\Validation\BaseValidator:
Lunar\Core\Exceptions\Carts\CartException is thrown. Errors are accessed the same way as Laravel’s own validation responses:
Modifiers
Modifiers hook fixed points in cart calculation without depending on the names of core’s steps. ALunar\Core\Modifiers\CartLineModifier hooks each line; a Lunar\Core\Modifiers\CartModifier hooks the cart as a whole.
Each hook runs the registered modifiers in registration order, as a pipeline. The hooks run inside
calculate(), so a modifier that prices something sees the same $cart->context() as the cart being calculated.
The hooks run only when the steps run. When calculate() serves the persisted totals snapshot instead, no hook fires: the snapshot already holds the totals the modifiers produced. Keep side effects out of modifiers, or call recalculate() where they must run. A modifier that records data beyond the standard totals writes it to the cart’s or line’s meta, which is stored on the row, rather than to a custom property on the model, which a served snapshot does not restore. See Totals caching.
A change to the subtotal in subtotalled() reaches discounts, tax and the cart total, since those are calculated from it:
calculating(Cart $cart, Closure $next): Cart and calculated(Cart $cart, Closure $next): Cart. Every hook has a pass-through default, so a modifier overrides only the hooks it needs.
Register an instance with the Lunar\Core\Modifiers\CartLineModifiers or Lunar\Core\Modifiers\CartModifiers collection, both singletons in the container:
ShippingModifier, is covered in Extending Shipping.
Overriding order creation
Cart::createOrder() delegates to Contracts\Carts\CreatesOrder, which in turn runs the order through the StepList::OrderCreation step list. See Extending Orders for the order creation steps, order modifiers and action contracts.