Problem/Motivation

Core builds Drupal\commerce\Context by hand in 9 places, and the copies have drifted: AvailabilityOrderProcessor and UnitPriceWidget leave out the time (so they use "now"), while OrderRefresh, OrderItemEditForm and BuyXGetY pass the order's calculation date.

Contrib has no single place to add data to the Context, and any new Context input (such as the organization, see #3638816: [META] Commerce 4.x roadmap) would have to be added to every call site by hand.

Proposed resolution

Add two services that build every Context in core, and an event both dispatch.

// commerce_store
interface CurrentContextInterface {
  // Context for the current request: current user, the given store or the
  // current store, current time. The base Context is memoized per request and store.
  public function getContext(?StoreInterface $store = NULL, array $data = []): Context;
  public function getCacheableMetadata(): CacheableMetadata;
}

// commerce_order
interface OrderContextFactoryInterface {
  // Context for an order: its customer, its store, getCalculationDate().
  // Deterministic, not memoized.
  public function createForOrder(OrderInterface $order, array $data = []): Context;
}
  • Event: both services dispatch ContextEvents::CREATE before building the Context. It exposes the source (request or the order), customer, store, time and a mutable $data array. Context stays final with getters only.
  • Caller data (e.g. field_name from the formatters) is merged after the event, so the event runs once per memoized base Context, not once per rendered price.
  • Cacheability: getCacheableMetadata() returns the user and store cache contexts; the formatters add it to their render arrays.
  • CurrentContextInterface lives in commerce_store because it needs commerce_store.current_store, and commerce does not depend on commerce_store.

Call sites

Call site Moves to
PriceCalculatedFormatter (commerce_order) CurrentContextInterface
PriceCalculatedFormatter (commerce_price) CurrentContextInterface
AddToCartForm::prepareOrderItem() CurrentContextInterface, with the store from selectStore()
PurchasedEntityAvailableConstraintValidator Both: without an order, CurrentContextInterface with the selected store; on a draft order, OrderContextFactoryInterface
OrderRefresh OrderContextFactoryInterface
AvailabilityOrderProcessor OrderContextFactoryInterface
BuyXGetY OrderContextFactoryInterface
OrderItemEditForm OrderContextFactoryInterface
UnitPriceWidget OrderContextFactoryInterface

Tax rate resolvers and order processors that need the Context inject OrderContextFactoryInterface and call createForOrder($order_item->getOrder()). OrderRefresh sets order_id->entity before the processors run, so this returns the order being refreshed. TaxRateResolverInterface::resolve() keeps its signature.

Remaining tasks

  • Kernel tests for both services: subscriber data, caller data merged after the event, memoization per store.
  • Test that createForOrder() uses getCalculationDate().
  • Existing price and availability tests stay green.

User interface changes

None.

API changes

New services, interfaces and event. The constructors of the classes that move get a new argument, with no BC layer (4.x). new Context() keeps working; nothing is deprecated.

Behavior change: AvailabilityOrderProcessor and UnitPriceWidget now use the order's calculation date. Nothing changes for draft orders (still the request time); for placed orders it is the placed time.

Data model changes

None.

Comments

jsacksick created an issue. See original summary.

jsacksick’s picture

Issue summary: View changes