Problem/Motivation

B2B sites price, check availability and select gateways per organization (a company buying through several users), but the pricing Context only carries the customer, the store and the time. B2B modules (commerce_b2b, price lists, promotions) need a generic organization API in core to code against.

Settling the Context shape and the organization interfaces in 4.0.0 lets contrib build on them from day one. This issue adds API only: no data model change, and core resolves no organization, so getOrganization() returns NULL until a module provides a resolver. The organization on orders and carts comes in a 4.x minor (see #3638816: [META] Commerce 4.x roadmap), including the organization_id field (see #3638816-14: [META] Commerce 4.x roadmap).

Proposed resolution

  • OrganizationInterface (commerce): implemented by the site's organization entity type, whichever module provides it (a Commerce submodule, commerce_b2b, a Group bridge). Extends EntityPublishedInterface, since an unpublished organization must not get carts.
  • Context: an optional trailing ?OrganizationInterface $organization = NULL constructor argument, getOrganization(), and an immutable withOrganization() returning a new instance (Context stays final). The organization is a dedicated property, not mirrored in data. The ContextEvents::CREATE event from #3639232: Add context services to build the pricing Context cannot replace it.
  • OrganizationResolverInterface + ChainOrganizationResolver: resolve the organization for an account and a store. Core ships no resolver; membership modules do.
  • CurrentOrganizationInterface: the organization for the current request, memoized; OrganizationEvents::SWITCH resets it when a user switches organization.
  • organization cache context: NULL hashes to a stable key, so B2C cache hit rates do not change.
  • CurrentContextInterface (#3639232: Add context services to build the pricing Context) sets the organization resolved for the store, and exposes the cache context plus the organization entity as a cacheable dependency, so a status or field change invalidates.
  • OrganizationOwnedInterface + OrganizationOwnedTrait (commerce): getOrganization(), getOrganizationId() and setOrganization() for entities owned by an organization, like EntityOwnerInterface / EntityOwnerTrait. OrderInterface extends it and Order uses the trait. The methods guard on the organization_id field, which the 4.x minor adds: until then getOrganization() returns NULL and setOrganization() throws.

Remaining tasks

  • Depends on #3639232: Add context services to build the pricing Context.
  • Settled: OrganizationInterface extends ContentEntityInterface and EntityPublishedInterface, with no other methods. OrganizationResolverInterface::resolve(AccountInterface $account, StoreInterface $store): ?OrganizationInterface; resolvers are tagged commerce.organization_resolver, run by priority, and the first non-NULL result wins.
  • Kernel tests with a test organization entity type and a test resolver: Context, current organization, cache context, switch event.

User interface changes

None.

API changes

New interfaces, services, event and cache context; Context gains an optional organization. OrderInterface extends OrganizationOwnedInterface, so a custom class implementing OrderInterface without extending Order must add its three methods (the trait provides them). Otherwise nothing breaks: existing new Context() calls keep working.

Data model changes

None.

Issue fork commerce-3639236

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jsacksick created an issue. See original summary.

jsacksick’s picture

Issue summary: View changes

Updated the summary: the OrganizationInterface methods and the resolver signature are settled, and the organization ownership follow-up (OrganizationOwnedInterface / OrganizationOwnedTrait, 4.x minor) is linked from #3638816-14: [META] Commerce 4.x roadmap.

jsacksick’s picture

Issue summary: View changes

Moved OrganizationOwnedInterface / OrganizationOwnedTrait into this issue: OrderInterface gains their methods, which is cleaner in 4.0.0 than in a minor. The organization_id field itself (the hook_entity_base_field_info(), OrganizationEntityTypeRepository, the install routine) stays in the 4.x minor, see #3638816: [META] Commerce 4.x roadmap.

jsacksick’s picture

Status: Active » Needs review