OrderReceiptMail.php in Commerce Core 8.2
File
modules/order/src/Mail/OrderReceiptMail.php
View source
<?php
namespace Drupal\commerce_order\Mail;
use Drupal\commerce\MailHandlerInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\OrderTotalSummaryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class OrderReceiptMail implements OrderReceiptMailInterface {
use StringTranslationTrait;
protected $mailHandler;
protected $orderTotalSummary;
protected $profileViewBuilder;
public function __construct(EntityTypeManagerInterface $entity_type_manager, MailHandlerInterface $mail_handler, OrderTotalSummaryInterface $order_total_summary) {
$this->mailHandler = $mail_handler;
$this->orderTotalSummary = $order_total_summary;
$this->profileViewBuilder = $entity_type_manager
->getViewBuilder('profile');
}
public function send(OrderInterface $order, $to = NULL, $bcc = NULL) {
$to = isset($to) ? $to : $order
->getEmail();
if (!$to) {
return FALSE;
}
$subject = $this
->t('Order #@number confirmed', [
'@number' => $order
->getOrderNumber(),
]);
$body = [
'#theme' => 'commerce_order_receipt',
'#order_entity' => $order,
'#totals' => $this->orderTotalSummary
->buildTotals($order),
];
if ($billing_profile = $order
->getBillingProfile()) {
$body['#billing_information'] = $this->profileViewBuilder
->view($billing_profile);
}
$params = [
'id' => 'order_receipt',
'from' => $order
->getStore()
->getEmail(),
'bcc' => $bcc,
'order' => $order,
];
$customer = $order
->getCustomer();
if ($customer
->isAuthenticated()) {
$params['langcode'] = $customer
->getPreferredLangcode();
}
return $this->mailHandler
->sendMail($to, $subject, $body, $params);
}
}