I am attempting to develop a module that will send an email to a customer in my Commerce 2 store when that customer's order's status is updated to "Completed". I am basing my module off of the example shown in the handbook (https://docs.drupalcommerce.org/commerce2/developer-guide/orders/workflows/react-to-workflow-transitions)
Here's what I have so far:
File: /modules/custom/tracking_number_email/tracking_number_email.info.yml
name: Commerce Tracking Number Email
type: module
description: Uses an event subscriber to send an email with tracking number to customer after order status has been updated.
package: Commerce
core: 8.x
File: /modules/custom/tracking_number_email/tracking_number_email.services.yml
services:
tracking_number_email.order_fulfillment_subscriber:
class: Drupal\tracking_number_email\EventSubscriber\OrderFulfillmentSubscriber
arguments: ['@language_manager', '@plugin.manager.mail']
tags:
- { name: event_subscriber }
File: /modules/custom/tracking_number_email/src/EventSubscriber/TrackingNumberEmail.php
<?php
namespace Drupal\tracking_number_email\EventSubscriber;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Sends an email when the order transitions to Fulfillment.
*/
class OrderFulfillmentSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The mail manager.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* Constructs a new OrderFulfillmentSubscriber object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* The mail manager.
*/
public function __construct(
LanguageManagerInterface $language_manager,
MailManagerInterface $mail_manager
) {
$this->languageManager = $language_manager;
$this->mailManager = $mail_manager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [
'commerce_order.fulfill.post_transition' => ['sendEmail', -100],
];
return $events;
}
/**
* Sends the email.
*
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event-
* The transition event.
*/
public function sendEmail(WorkflowTransitionEvent $event) {
// Create the email.
$order = $event->getEntity();
$to = $order->getEmail();
$params = [
'from' => $order->getStore()->getEmail(),
'subject' => $this->t(
'Regarding your order [#@number]',
['@number' => $order->getOrderNumber()]
),
'body' => ['#markup' => $this->t(
'Your order with #@number that you have placed with us has been processed and is awaiting fulfillment.',
['@number' => $order->getOrderNumber()]
)],
];
// Set the language that will be used in translations.
if ($customer = $order->getCustomer()) {
$langcode = $customer->getPreferredLangcode();
}
else {
$langcode = $this->languageManager->getDefaultLanguage()->getId();
}
// Send the email.
$this->mailManager->mail('commerce', 'receipt', $to, $langcode, $params);
}
}
I am receiving the following error message when this custom module is installed:
ReflectionException: Class Drupal\tracking_number_email\EventSubscriber\OrderFulfillmentSubscriber does not exist in ReflectionClass->__construct() (line 30 of /home1/mysite/public_html/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterEventSubscribersPass.php).
If I remove the tracking_number_email.services.yml the error goes away, but the module doesn't work (to be clear, it doesn't work whether or not that services.yml file is in place). I assume I have something erroneous in that services.yml file, but I am stuck trying to figure out what is wrong.