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). ExtendsEntityPublishedInterface, since an unpublished organization must not get carts.Context: an optional trailing?OrganizationInterface $organization = NULLconstructor argument,getOrganization(), and an immutablewithOrganization()returning a new instance (Contextstays final). The organization is a dedicated property, not mirrored indata. TheContextEvents::CREATEevent 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::SWITCHresets it when a user switches organization.organizationcache 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()andsetOrganization()for entities owned by an organization, likeEntityOwnerInterface/EntityOwnerTrait.OrderInterfaceextends it andOrderuses the trait. The methods guard on theorganization_idfield, which the 4.x minor adds: until thengetOrganization()returns NULL andsetOrganization()throws.
Remaining tasks
- Depends on #3639232: Add context services to build the pricing Context.
- Settled:
OrganizationInterfaceextendsContentEntityInterfaceandEntityPublishedInterface, with no other methods.OrganizationResolverInterface::resolve(AccountInterface $account, StoreInterface $store): ?OrganizationInterface; resolvers are taggedcommerce.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
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
Comment #2
jsacksick commentedUpdated the summary: the
OrganizationInterfacemethods 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.Comment #3
jsacksick commentedMoved
OrganizationOwnedInterface/OrganizationOwnedTraitinto this issue:OrderInterfacegains their methods, which is cleaner in 4.0.0 than in a minor. Theorganization_idfield itself (thehook_entity_base_field_info(),OrganizationEntityTypeRepository, the install routine) stays in the 4.x minor, see #3638816: [META] Commerce 4.x roadmap.Comment #5
jsacksick commented