On this page
- How can I modify the available Conditions on the order document configuration page?
- Removing displayed Condition
- How can I control the list of Documents displayed on the Order Documents page?
- How can I send documents automatically to customers in response to various events?
- How can I provide customer-facing links for viewing/downloading order documents?
Additional customization
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:
- Load all order documents that are enabled for the order's type (bundle).
- Dispatch the
Drupal\commerce_order_document\Event\DocumentEvents::FILTER_ORDER_DOCUMENTS event to allow the list to be altered. - For each order document:
- Use the order document plugin
readyForOrdermethod to remove any documents that are "not ready." - Use the order document plugin
appliesmethod to filter based on the configured Conditions logic for the order document.
- Use the order document plugin
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.
How can I provide customer-facing links for viewing/downloading order documents?
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
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.