0

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;
    }
}
3
  • 1
    ok alll things will come form the $order = $event->getEntity(); ... the shipping is in user profile, helpfull to install the devel module then you can inspect the order i like to use symphony var dumper as (devel setting) dsm sucks
    – taggartJ
    Dec 24 '19 at 0:04
  • 1
    Commerce also has a MailHandler that makes sending emails easier: drupal.org/node/3030431 Shipping information is in $order->shipments, and sometimes Devel chokes, setting up xdebug is often a worthy time investment. Dec 27 '19 at 11:33
  • Bojan Zivanovic, it was as you said: shipments... how do you go from here though in order to find the right info by using devel? do you dpm($order->shipments) ? or dpm($order->shipments->getValue()) ? how do you know which one to go for? how do you choose how to go deeper? array vs object? or when to go for ->entity? i know those are a lot of questions but the documentation on HOW TO DEBUG is so bad for Drupal 8, and commerce in particular? or do you look in the commerce module code at the class and methods? trying to get a better idea on how to go from point A to point B when debugging
    – Nathan
    Dec 31 '19 at 17:38
3

You can have the shipping information with :

$shippement = $order->shipments->first()->entity;

$shipping_profile = $shippement->shipping_profile->first()->entity;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.