You are here

class OrderReceiptSubscriber in Commerce Core 8.2

Sends a receipt email when an order is placed.

Hierarchy

  • class \Drupal\commerce_order\EventSubscriber\OrderReceiptSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of OrderReceiptSubscriber

1 string reference to 'OrderReceiptSubscriber'
commerce_order.services.yml in modules/order/commerce_order.services.yml
modules/order/commerce_order.services.yml
1 service uses OrderReceiptSubscriber
commerce_order.order_receipt_subscriber in modules/order/commerce_order.services.yml
Drupal\commerce_order\EventSubscriber\OrderReceiptSubscriber

File

modules/order/src/EventSubscriber/OrderReceiptSubscriber.php, line 13

Namespace

Drupal\commerce_order\EventSubscriber
View source
class OrderReceiptSubscriber implements EventSubscriberInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The order receipt mail.
   *
   * @var \Drupal\commerce_order\Mail\OrderReceiptMailInterface
   */
  protected $orderReceiptMail;

  /**
   * Constructs a new OrderReceiptSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce_order\Mail\OrderReceiptMailInterface $order_receipt_mail
   *   The mail handler.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, OrderReceiptMailInterface $order_receipt_mail) {
    $this->entityTypeManager = $entity_type_manager;
    $this->orderReceiptMail = $order_receipt_mail;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_order.place.post_transition' => [
        'sendOrderReceipt',
        -100,
      ],
    ];
    return $events;
  }

  /**
   * Sends an order receipt email.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The event we subscribed to.
   */
  public function sendOrderReceipt(WorkflowTransitionEvent $event) {

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $event
      ->getEntity();
    $order_type_storage = $this->entityTypeManager
      ->getStorage('commerce_order_type');

    /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
    $order_type = $order_type_storage
      ->load($order
      ->bundle());
    if ($order_type
      ->shouldSendReceipt()) {
      $this->orderReceiptMail
        ->send($order, $order
        ->getEmail(), $order_type
        ->getReceiptBcc());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OrderReceiptSubscriber::$entityTypeManager protected property The entity type manager.
OrderReceiptSubscriber::$orderReceiptMail protected property The order receipt mail.
OrderReceiptSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
OrderReceiptSubscriber::sendOrderReceipt public function Sends an order receipt email.
OrderReceiptSubscriber::__construct public function Constructs a new OrderReceiptSubscriber object.