You are here

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;

  /**
   * The mail handler.
   *
   * @var \Drupal\commerce\MailHandlerInterface
   */
  protected $mailHandler;

  /**
   * The order total summary.
   *
   * @var \Drupal\commerce_order\OrderTotalSummaryInterface
   */
  protected $orderTotalSummary;

  /**
   * The profile view builder.
   *
   * @var \Drupal\profile\ProfileViewBuilder
   */
  protected $profileViewBuilder;

  /**
   * Constructs a new OrderReceiptMail object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\MailHandlerInterface $mail_handler
   *   The mail handler.
   * @param \Drupal\commerce_order\OrderTotalSummaryInterface $order_total_summary
   *   The order total summary.
   */
  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');
  }

  /**
   * {@inheritdoc}
   */
  public function send(OrderInterface $order, $to = NULL, $bcc = NULL) {
    $to = isset($to) ? $to : $order
      ->getEmail();
    if (!$to) {

      // The email should not be empty.
      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);
  }

}

Classes

Namesort descending Description
OrderReceiptMail