Skip to content

Instantly share code, notes, and snippets.

@mattc321
Created June 19, 2018 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattc321/b0183436b8aacd560135ca521809ff08 to your computer and use it in GitHub Desktop.
Save mattc321/b0183436b8aacd560135ca521809ff08 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\afc_commerce\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.place.post_transition' => ['sendEmail', -100],
];
return $events;
}
/**
* Sends the email.
*
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
* The transition event.
*/
public function sendEmail(WorkflowTransitionEvent $event) {
\Drupal::logger('afc_commerce')->notice('fired sendEmail post_trans');
$order = $event->getEntity();
$query = \Drupal::database()->select('commerce_order', 'c');
$query->fields('p', ['field_customer_email_value']);
$query->join('profile__field_customer_email', 'p', 'p.entity_id = c.billing_profile__target_id');
$query->condition('c.order_id', $order->id());
$result = $query->execute();
while ($row = $result->fetchAssoc()) {
$email_to = $row['field_customer_email_value'];
}
//add case for empty, skip email
$to = $email_to;
$params = [
'from' => $order->getStore()->getEmail(),
'subject' => $this->t(
'Regarding your order [#@number]',
['@number' => $order->getOrderNumber()]
),
'body' => $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();
}
//way to invoke the existing receipt template from themes/afc
$this->mailManager->mail('commerce_order', 'receipt', $to, $langcode, $params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment