Lunar\Core\Facades\StorefrontSession tracks the selections that frame a storefront visit — region, channel, currency, customer, and customer groups — in the session, and can produce an immutable StorefrontContext snapshot of them for code that should not reach into the session directly.
Overview
StorefrontSession is backed by Lunar\Core\Managers\StorefrontSessionManager, bound to the Lunar\Core\Contracts\StorefrontSession contract. It is resolved once per request (or job): its constructor restores region, channel, customer groups, currency, and customer from the session, falling back to each primitive’s configured default when nothing is stored yet.
v1 exposed a
Lunar\Facades\StorefrontSession with init*() methods (initChannel(), initCurrency(), initCustomerGroups(), initCustomer()) that had to be called to restore session state. In v2 this restoration runs automatically in the manager’s constructor — the init*() methods still exist and are safe to call, but they are no-ops once a value is already resolved.What it holds
Setting a region also sets the channel and currency from it (a region belongs to a channel and has a default currency), unless overridden afterwards by an explicit
setChannel() / setCurrency() call.
Setting values
handle, channel by handle, currency by code, customer by id) so it survives to the next request.
Setting the currency also pushes it to Lunar\Core\Facades\CartSession, so the visitor’s cart (if any) is reprised in the new currency — a storefront currency switch has to reach the cart, since the cart carries its own currency_id and prices from it.
Resetting customer groups
initCustomerGroups()).
Groups set with setCustomerGroups() / setCustomerGroup(), or restored from the session, count as chosen and are passed to context() as-is. Until groups are chosen (or after a reset), context() derives them from the session’s customer instead, so a logged-in customer is priced with their own groups.
Clearing the session
Resolving a context
context(): StorefrontContext bundles the session’s current selections — channel, currency, language, region, customer, and customer groups — into Lunar\Core\DataObjects\StorefrontContext, an immutable value object. It exists so business logic that needs “the buyer’s selections” can accept an explicit value instead of depending on there being an HTTP session at all: a queued job pricing a catalogue for a market, an API resolving selections from headers, or a test exercising pricing for a given currency and customer group.
Lunar\Core\Models\Cart exposes the same shape via $cart->context(), built from the cart’s own stored channel, currency, customer, and region — a cart is already a context, so post-checkout logic and pre-cart browse logic consume the same explicit type.
Feeding pricing
Pricing::using() prices against the whole context (currency, customer groups, customer, region, and channel) in one call, in place of chaining currency(...)->customerGroups(...). Cart calculation does not read the session: it prices each line with $cart->context(), built from the cart’s own stored values.
Using the session as the default pricing context
A bare price call, with no context passed, prices against store defaults: the default region, its channel and currency, the default customer group, and no customer. The pricing manager never reads the session or the auth guard itself.useAsDefaultContext() makes the session the default for the current request, so bare calls price for the current visitor’s currency, region, customer, and customer groups:
context() is resolved on each bare get(), not when useAsDefaultContext() is called, so a login or currency switch earlier in the same request is seen. An explicitly supplied context (using($context) or pricing($context)) always takes priority over the default.
Call it from a middleware on the web group:
web group also serves the admin panel and any other routes in the application. A session-less storefront API can register its own resolver instead, through Lunar\Core\Contracts\DefaultStorefrontContext::resolveUsing().
Outside a request (queued jobs, console commands, scheduled tasks) nothing is registered, so bare calls price against store defaults. Pass a context explicitly there: from the model in hand ($cart->context()) or one built with Lunar\Core\Contracts\Actions\Storefront\ResolvesStorefrontContext.
Catching missing contexts
Pricing::preventDefaultContext() makes get() throw Lunar\Core\Exceptions\MissingStorefrontContextException when no context was supplied through using() or pricing($context) and no default resolver is registered for the request. The individual setters (currency(), customerGroups()) do not count as a context. Enable it outside production from a service provider:
useAsDefaultContext() are unaffected. What it surfaces is a job, command, or route that prices against store defaults by accident.
Session-scoped, not a singleton
Lunar\Core\Contracts\StorefrontSession (like Lunar\Core\Contracts\CartSession, which it depends on) is bound scoped in the container, not singleton. Under Octane or a queue worker a singleton binding would persist across requests, leaking one visitor’s region, currency, or customer into the next request served by the same worker. scoped memoizes the resolved selections for the life of one request or job and is discarded when it ends.