The Goal
Administrative users creating and updating orders in the backend may want to:
- Preview the order receipt before emailing customers.
- Send a copy of the order receipt to an email address other than the contact email address for the order.
Developers may want to:
- Preview the order receipt for testing purposes when working on environments with outgoing emails disabled
A Solution
We can add a button to the right of the existing, "Resend receipt" button that appears on Order View pages, like this:
When clicked, a fully rendered order receipt should be displayed. If an administrative user wants to email the receipt to an address other than the order contact email, the user can copy and paste the text into an email message.
Our implementation involves three components:
- A custom Controller class
- A routing definition for our controller
- A custom action link for the button
Implementation:
Create a custom controller method that builds and returns a render array identical to the one created in the Commerce Core Drupal\commerce_order\Mail\OrderReceiptMail::send()
method:
$body = [
'#theme' => 'commerce_order_receipt',
'#order_entity' => $commerce_order,
'#totals' => $this->orderTotalSummary->buildTotals($commerce_order),
];
if ($billing_profile = $commerce_order->getBillingProfile()) {
$body['#billing_information'] = $this->profileViewBuilder->view($billing_profile);
}
return $body;
Create a custom route for the controller method:
- The
path
should not begin with/admin/
so that standard theming will be used. - The path will take
{commerce_order}
as a parameter. - Require the
administer commerce_order
permission.
Add the button to the order view page by creating an action link for the custom route:
- The
appears_on
value should beentity.commerce_order.canonical
Comments