So far I have a working piece of code that sends me an email ($to variable) when a new order has been placed on my drupal 8 commerce. This is not the email the person that places the order gets.
As part of the body of the email, i was able to find and use variables like a custom attribute, sku, quantity, product name etc...
I realize that in the orderCompleteHandler(), i might have to change what i send as a parameter, instead of $items having to possibly send $order.
But where can i find/how can i get the shipping information, like shipping address, shipping date and the user's that placed the order name : I would like to add them to the email i send.
More importantly is also: how do i find all this info and others by myself? i tried to kint($order) and it either freezes or runs out of memory. I tried dpm($order) and i get 10K lines of info but nothing that is obvious. I tried to look in those 10k lines for some unique data that i knew was part of the shipping info but it wont show up, most likely because that value will be the return value of a get function.
Basically, how should i look and where should i look if i want various information from the order placed variable aka $order?
Any help in debugging and pointing me in the right direction would be appreciated.
public function orderCompleteHandler(WorkflowTransitionEvent $event) {
// @var \Drupal\commerce_order\Entity\OrderInterface $order
$order = $event->getEntity();
$items = $order->getItems();
$mail_manager = \Drupal::service('plugin.manager.mail');
$to = 'myEmail@gmail.com';
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$type = 'order_placed';
$mail_manager->mail('my_custom_order', $type, $to, $langcode, $items, NULL, TRUE);
}
/**
* Implements hook_mail()
* Prepares emails sent by the MailHandler service.
* Emails are sent within the OrderComplete Event Subscriber.
*/
function my_custom_order_mail($key, &$message, $params) {
switch($key) {
case 'order_placed':
$options = array(
'langcode' => $message['langcode'],
);
// Creating an html formatted email.
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
$message['from'] = 'staff@myCompany';
$message['subject'] = t('Order @order_number confirmed', array('@order_number' => $params[0]->getOrderId()), $options);
$entire_email_display = '';
foreach ($params as $product) {
// Get the Custom Location.
$productLocation = $product->getPurchasedEntity()->getAttributeValueIds()['attribute_custom_location'];
$order_item['sku'] = $product->getPurchasedEntity()->getSku();
$order_item['quantity'] = (int)$product->getQuantity();
$order_item['product_name'] = $product->getPurchasedEntity()->getTitle();
// HERE I BUILT an entire email body and stored in the $entire_email_display.
}
$message['body'][] = $entire_email_display;
}
}