diff --git a/modules/order/commerce_order.services.yml b/modules/order/commerce_order.services.yml index b3bc97a..a46f601 100644 --- a/modules/order/commerce_order.services.yml +++ b/modules/order/commerce_order.services.yml @@ -45,3 +45,7 @@ services: commerce_order.order_total_summary: class: Drupal\commerce_order\OrderTotalSummary arguments: ['@plugin.manager.commerce_adjustment_type'] + + entity_print.renderer.commerce_order: + class: Drupal\commerce_order\Renderer\OrderEntityRenderer + arguments: ['@renderer', '@entity_print.asset_renderer', '@event_dispatcher', '@entity_type.manager', '@commerce_order.order_total_summary'] diff --git a/modules/order/src/Renderer/OrderEntityRenderer.php b/modules/order/src/Renderer/OrderEntityRenderer.php new file mode 100644 index 0000000..6f75b9f --- /dev/null +++ b/modules/order/src/Renderer/OrderEntityRenderer.php @@ -0,0 +1,91 @@ +orderTotalSummary = $order_total_summary; + $this->profileViewBuilder = $entity_type_manager->getViewBuilder('profile'); + } + + /** + * {@inheritdoc} + */ + public function render(array $orders) { + return array_map([$this, 'renderSingle'], $orders); + } + + /** + * {@inheritdoc} + */ + protected function getLabel(EntityInterface $entity) { + return t('Order @id receipt', ['@id' => $entity->label()]); + } + + /** + * Builds a print ready render array for a single order. + * + * @param \Drupal\commerce_order\Entity\OrderInterface $order + * The order entity. + * + * @return array + * The render array. + */ + protected function renderSingle(OrderInterface $order) { + // Use the order receipt template for Entity Print render. + $build = [ + '#theme' => 'commerce_order_receipt', + '#order_entity' => $order, + '#totals' => $this->orderTotalSummary->buildTotals($order), + ]; + + if ($billing_profile = $order->getBillingProfile()) { + $build['#billing_information'] = $this->profileViewBuilder->view($billing_profile); + } + + return $build; + } + +}