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

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.

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 the Lunar\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() or boot(); changing a list during a request is unsupported.
  • before(), after() or replace() with an anchor that is not in the list throws Lunar\Core\Exceptions\UnknownStepException when 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:
Step lists are not read from config. The lunar.cart.pipelines, lunar.cart.validators, lunar.cart.prune_tables.pipelines, lunar.pricing.pipelines and lunar.orders.pipelines keys no longer exist. If one is still defined, the first read of any step list throws Lunar\Core\Exceptions\RemovedStepConfigException, naming the key and the Steps::for() call that replaces it. Remove the key; php artisan lunar:upgrade prints the equivalent Steps calls for a customized list.

Writing a cart step

A CartCalculation 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:
To keep writing the snapshot but never serve it, set lunar.cart.totals.ttl to 0 instead. No binding is needed.
Code that writes carts, cart_lines, or cart_addresses rows without going through the model (a query-builder update() or a raw statement) bypasses the observers that mark the snapshot stale. Call $cart->invalidateTotals() after such a write, otherwise calculate() can serve totals that predate it for up to lunar.cart.totals.ttl seconds.

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:
Register it against the relevant list:
If validation fails, 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. A Lunar\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.
calculating() runs before GetUnitPrice, which always sets the line’s unit price, so a unit price set in calculating() is overwritten. Change a line’s price in subtotalled(), or register a step after GetUnitPrice in the CartLinePricing list.
A change to the subtotal in subtotalled() reaches discounts, tax and the cart total, since those are calculated from it:
A cart modifier implements 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:
Use a modifier for “after the subtotal” or “after calculation” logic. Register a step when the logic has to sit between two particular core steps. The shipping equivalent, 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.