Change record status: 
Project: 
Introduced in branch: 
4.x
Introduced in version: 
4.0.0
Description: 

Commerce 4.0.0 can scope orders, carts and pricing to an organization (a company buying through the store). Commerce does not ship an organization entity type: a B2B module provides it (commerce_b2b's commerce_organization submodule, a Group bridge or a custom entity type). Nothing changes for sites without one.

Organization API

  • Drupal\commerce\OrganizationInterface: implemented by the organization entity class (a published, content entity).
  • OrderInterface now extends Drupal\commerce\OrganizationOwnedInterface (getOrganization(), getOrganizationId(), setOrganization()). A custom class implementing OrderInterface without extending Order must add these methods, for example with OrganizationOwnedTrait.
  • Organization resolvers implement Drupal\commerce\Resolver\OrganizationResolverInterface::resolve(AccountInterface $account, ?StoreInterface $store) and are tagged commerce.organization_resolver. The chain (commerce.chain_organization_resolver) skips unpublished organizations.
  • Drupal\commerce_store\CurrentOrganizationInterface::getOrganization() returns the organization the current user acts for in the current store, resolved once per request. The organization cache context follows it.
  • The pricing Context carries the organization: Context::getOrganization(), Context::withOrganization().
  • Code changing which organization a user acts for (an organization switcher) dispatches OrganizationEvents::SWITCH (OrganizationSwitchEvent), which resets the current organization.

Organization plumbing

Once an organization entity type exists, Commerce does the following.

The organization entity type

OrganizationEntityTypeRepositoryInterface::getEntityTypeId() (commerce.organization_entity_type_repository) returns the site's organization entity type: the one entity type implementing OrganizationInterface. With several, it returns NULL and the status report shows an error; uninstall all but one. Once the organization_id field is installed, its target is fixed: installing another organization module later does not change it.

The organization_id field

Commerce adds an organization_id base field to every entity type implementing OrganizationOwnedInterface (orders today), targeting that entity type, and installs or uninstalls it when the organization module is installed or uninstalled. A module whose entity class starts implementing OrganizationOwnedInterface runs the field installer from an update hook:

function mymodule_update_10001() {
  \Drupal::service('commerce.organization_field_installer')->installFieldStorage();
}

Fields targeting organizations

A module referencing organizations from its own entity type (a price list targeting organizations) defines the field in baseFieldDefinitions(), targeting the repository's entity type, and keeps working on Commerce 3:

if (\Drupal::hasService('commerce.organization_entity_type_repository')) {
  $target_type = \Drupal::service('commerce.organization_entity_type_repository')->getEntityTypeId();
  if ($target_type) {
    $fields['organizations'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Organizations'))
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      ->setSetting('target_type', $target_type);
  }
}

Commerce installs and uninstalls such fields when the organization module is installed or uninstalled. When adding the field to existing sites, call installFieldStorage() from the update hook (guarded by \Drupal::hasService('commerce.organization_field_installer')), not installFieldStorageDefinition(): Commerce may already have installed it.

Carts

  • New carts get the organization the customer acts for in the store, and assigning an order to a customer sets theirs (or none).
  • CartProvider lookups are scoped to that organization: a user gets one cart per organization, plus personal carts (without an organization). Switching organization changes which carts are returned. CartProviderInterface keeps its signatures.
  • Carts of an unpublished organization are no longer eligible.

Order access

Access to an order with an organization is no longer granted from its uid: "view own commerce_order", cart access, order query access and checkout exclude organization orders. The module managing memberships grants access to those, with hook_commerce_order_access() and its own query access condition (entity.query_access.commerce_order event).

Organization condition

An "Organization" condition (Customer category) matches orders whose organization is, or is not, one of the selected ones, for promotions, payment gateways and shipping methods. It is only offered when the site has an organization entity type.

Impacts: 
Site builders, administrators, editors
Module developers
Site templates, recipes and distribution developers