Additional customization

Last updated on
26 May 2023

How can I modify the available Conditions on the order document configuration page?

Removing displayed Condition

The Commerce Order Document module includes an event subscriber to remove the Order Type Condition from the available options. A similar event subscriber can be implemented to remove any other displayed Condition option. The relevant event is:

  • Drupal\commerce\Event\CommerceEvents::FILTER_CONDITIONS (commerce.filter_conditions)

See Drupal\commerce_order_document\EventSubscriber\FilterConditionsEventSubscriber

Adding new Condition

The Drupal Commerce module defines the @CommerceCondition plugin type and includes many example implementations within its source code. The Commerce Order Document module includes all conditions defined for commerce_order entity type:

 *   entity_type = "commerce_order",

Create any custom  @CommerceCondition plugin defined for the commerce_order entity type to add it to the list of available Conditions for Order Document configuration. If you do not want to make the custom condition available in other contexts (Promotions, Payment gateways, etc.), use the FILTER_CONDITIONS event described above to limit its availability.

How can I control the list of Documents displayed on the Order Documents page?

The Drupal\commerce_order_document\OrderDocumentStorage::loadMultipleForOrder method provides the list of document options displayed on the order documents page. It uses the following approach:

  1. Load all order documents that are enabled for the order's type (bundle).
  2. Dispatch the Drupal\commerce_order_document\Event\DocumentEvents::FILTER_ORDER_DOCUMENTS event to allow the list to be altered.
  3. For each order document:
    1. Use the order document plugin readyForOrder method to remove any documents that are "not ready."
    2. Use the order document plugin applies method to filter based on the configured Conditions logic for the order document.

Based on that, there are multiple options for altering the list of Documents. First, you could implement a custom event subscriber to pre-filter the list before other checks are applied. Alternatively, you can implement the readyForOrder method to control document availability based on properties of the order. For example, you can use readyForOrder method if a document should only be available when an order is in specific states. If neither of those approaches, nor configured conditions gets you what you need, you could override the OrderDocumentStorage class with a custom one. Use hook_entity_type_alter() to swap out the standard order document storage class with your custom one, like this:

  if (isset($entity_types['commerce_order_document'])) {
    $entity_types['commerce_order_document']->setStorageClass(MyOrderDocumentStorage::class);
  }

How can I send documents automatically to customers in response to various events?

The Commerce Order Document module includes an event subscriber for all order transition events (post transitions). On every transition event, it loads all available order documents (using the same logic described above, for generating the list that appears on an order Documents page.) For each, it uses the order document's plugin method sendOnOrderTransitionId to check for applicability. So the simplest approach is to implement that method for an order document plugin.

If you want to send documents based on other events (such as the order-paid event), you can implement a similar event subscriber. Use the plugin method sendDocumentEmail to actually send the email.

You'll need to define custom routes and implement controller methods to handle the requests. Restrict access to the order's owner (customer). The methods should load the appropriate order document entity for the order and then use its plugin method to generate the documents:

  • Use buildOrderDocument($order) to display a document (returns a render array).
  • Use downloadDocument($order) to provide a document download (returns a streamed response).

Help improve this page

Page status: No known problems

You can: