Discussions

Make rule that also sends an email notification to store admin as well as to the customer when order placed

I would like to make rule that also sends an email notification to store admin as well as to the customer when order placed.

I have looked under (Below) to get any idea of how this may be done. But I did not see anything I could learn from.
Home » Administration » Store » Configuration » Checkout settings » Checkout rules » Editing reaction rule "Send an order notification e-mail"

Posted: Sep 14, 2011

Comments

Prince Manfred on September 15, 2011

Once you're editing "Send an order notification e-mail" look at the Actions section and click "+ Add action". In the drop-down box on the next page, under the System heading, select "Send mail". Now you should have a page where you can fill out the information you want in the email. For the value of "TO" enter "[site:mail]" without the quotes. For future reference (if you don't already know), if you click "Replacement patterns" under any given value box, it will give you a whole bunch of different variables to use related to your task. Just read the descriptions to see what they do. Fill in the rest of the boxes on the page with whatever information you want in them. Pick through the replacement patterns to find any information about the order you might want to include in the email message.

Random observation:

The replacement patterns [commerce-order:commerce-customer-shipping] and [commerce-order:commerce-customer-billing] only return id numbers that reference the actual address information for the order. [commerce-order:commerce_customer_shipping] and [commerce-order:commerce_customer_shipping] (notice they have underscores instead of dashes) will actually print out the shipping and billing address for you (though it's formatted a bit ugly. I don't have any easy solution for you on that but maybe you'll be able to live with it.)

Hope this is helpful. Let me know if I explained something poorly.

commercestudy on September 15, 2011

I am fresh to understanding rules. So rules looks a bit daunting to me. But your explanation was easy to follow from start to finish and gives me a better understanding of rules.

I tried

A customer has put an order ([commerce-order:created]) for a [commerce-order:type].

Their details are:
[commerce-order:commerce_customer_shipping]

which came out in the email as

A customer has put an order (Thu, 09/15/2011 - 23:23) for a commerce_order.

Their details are:
[commerce-order:commerce_customer_shipping]

Don't know why. But the rule did what is was supposed to which is the main thing.

Thankyou very much for the excellent tutorial

Prince Manfred on September 15, 2011

What information do you need the email to contain? If you need a list of what was purchased I'm afraid there isn't a useful replacement pattern for you. An email with that info is on my list of things to do for a site I'm developing. I'll post my solution once I get it done.

Prince Manfred on September 15, 2011

What information do you need the email to contain? If you need a list of what was purchased I'm afraid there isn't a useful replacement pattern for you. An email with that info is on my list of things to do for a site I'm developing. I'll post my solution once I get it done.

neardark on September 15, 2011

That's good to know about using underscores in place of the dashes on the token. Good tip!

I ended up finally just writing a custom module that parses the last placed order and e-mails it. I'll try and polish it up and post some code. Ideally it should be done as rule, but I had to do some serious formatting including a complex html with ticket images based on some products being a ticket.

John / NEAR DARK

Prince Manfred on September 15, 2011

If you have the PHP filter module enabled you can directly interact with the order that has triggering the email event rule with php. For example, placing the following in the message field...

<?php
 
$wrapper
= entity_metadata_wrapper('commerce_order', $commerce_order);
$billing_address = $wrapper->commerce_customer_billing->commerce_customer_address->value();
$shipping_address = $wrapper->commerce_customer_shipping->commerce_customer_address->value();

echo
"Shipping Address:";
echo
"\n".$shipping_address['name_line'];
echo
"\n".$shipping_address['thoroughfare'];
echo (
trim($shipping_address['premise']) != "" ? "\n".$shipping_address['premise'] : "");
echo
"\n".$shipping_address['locality'].", ".$shipping_address['administrative_area']."  ".$shipping_address['postal_code'];
echo
"\n\nBilling Address:";
echo
"\n".$billing_address['name_line'];
echo
"\n".$billing_address['thoroughfare'];
echo (
trim($billing_address['premise']) != "" ? "\n".$billing_address['premise'] : "");
echo
"\n".$billing_address['locality'].", ".$billing_address['administrative_area']."  ".$billing_address['postal_code'];
?>

...will print out the shipping and billing address entered in the order. The $commerce_order variable has all the information you could possibly want to email. I'm working up an email that will have the products ordered, quantity of products, price, order status, and address info in it. Once I'm done I can post it here if y'all would like.

commercestudy on September 15, 2011

"I'm working up an email that will have the products ordered, quantity of products, price, order status, and address info in it. Once I'm done I can post it here if y'all would like"

Yes this sounds really good. Order details and customer name and address are what is needed.

Prince Manfred on November 21, 2011

You can access the products. The $commerce_order variable will have info on all of the line items. Have you looked at trying Commerce Email (http://drupal.org/project/commerce_email) for doing this? It turns out it's usually better to avoid using the php filter if possible. Commerce Email will let you send an email with a nice little table containing the contents of the order.

Prince Manfred on September 17, 2011

The following code will only work for very specific situations. Your products must be of a product type with the name "product". It assumes that "product" has the fields "sku" and "title". If your product type has a different name, find the line "if($line_item->type == 'product'){" and change "product" to the machine name of your product.

I'm new to Drupal programming so I'm not very familiar with the multilingual features. I've hard coded using the 'und' language identifier for a few areas in this code. It'd be nice if someone could fill me in on best practice for that kind of thing.

This code will display the sku, title, quantity purchased of each product, billing address, and shipping address.

<?php
 
$wrapper
= entity_metadata_wrapper('commerce_order', $commerce_order);
$billing_address = $wrapper->commerce_customer_billing->commerce_customer_address->value();
$shipping_address = $wrapper->commerce_customer_shipping->commerce_customer_address->value();
$line_items $wrapper->commerce_line_items->value();

foreach(
$line_items as $line_item){
   if(
$line_item->type == 'product'){
     
$product = commerce_product_load($line_item->commerce_product['und'][0]['product_id']);
      echo
"\n".$product->sku." -- ".$product->title." -- (x".number_format($line_item->quantity).")";
   }
}

echo
"\n\nShipping Address:";
echo
"\n".$shipping_address['name_line'];
echo
"\n".$shipping_address['thoroughfare'];
echo (
trim($shipping_address['premise']) != "" ? "\n".$shipping_address['premise'] : "");
echo
"\n".$shipping_address['locality'].", ".$shipping_address['administrative_area']."  ".$shipping_address['postal_code'];
echo
"\n\nBilling Address:";
echo
"\n".$billing_address['name_line'];
echo
"\n".$billing_address['thoroughfare'];
echo (
trim($billing_address['premise']) != "" ? "\n".$billing_address['premise'] : "");
echo
"\n".$billing_address['locality'].", ".$billing_address['administrative_area']."  ".$billing_address['postal_code'];
?>

tpaddison on April 13, 2012

Thank you for posting this code. The billing address shows up perfectly for my emails. But when I try to add custom line item fields I get a printout of "array". Is there something wrong with the following code?

<?php
$wrapper
= entity_metadata_wrapper('commerce_order', $commerce_order);
$billing_address = $wrapper->commerce_customer_billing->commerce_customer_address->value();
$line_items $wrapper->commerce_line_items->value();

foreach(
$line_items as $line_item){
   if(
$line_item->type == 'patient_bill'){
     
$product = commerce_product_load($line_item->commerce_product['und'][0]['product_id']);
      echo
"\n".$product->sku." -- ".$product->title." -- (x".number_format($line_item->quantity).")";
      echo
"\n".$line_item->field_account_number;
   }
}

echo
"\n\nBilling Address:";
echo
"\n".$billing_address['name_line'];
echo
"\n".$billing_address['thoroughfare'];
echo (
trim($billing_address['premise']) != "" ? "\n".$billing_address['premise'] : "");
echo
"\n".$billing_address['locality'].", ".$billing_address['administrative_area']."  ".$billing_address['postal_code'];
?>

tpaddison on April 16, 2012

Oops! Should have looked more into this before I posted. I'm a beginner at php so I thought there was a syntax error-- but I found the right variable by using print_r.

Thanks again for posting your code so I knew how to even start my email.

flerkmisk on September 30, 2012

Thank you for this code, it helped me a lot!
Is there any way to add the total price to each product line?

For example what i mean: 011 -- Product -- (x2): $20

David C on October 3, 2012

I am stuck on trying to get 2 more items into mine. A custom text feild and the order totyal amount and its sub componets if available. Any hpointers in the right direction would be helpfull

stephan on December 23, 2012

Thanks for everyone 'above' who gave me a perfect starting point for my own email. That said, I have spent couple hours to hack together the following code. It's just there to help others with yet another example. Ultimately, I think the Commerce guys should really provide a default rule that 'just works'.

Also note, the email compiled here is only sent to our 'order manager'. So I put emphasis on completeness, not style.

<?php
$wrapper
= entity_metadata_wrapper('commerce_order', $commerce_order);
$shipping_address = $wrapper->commerce_customer_shipping->commerce_customer_address->value();
$line_items $wrapper->commerce_line_items->value();
$shipping_total = $wrapper->commerce_order_total->value();

# Get Packaging costs plus currency
$shipping_total = commerce_currency_format(
                                 
$commerce_order->commerce_order_total['und'][0]['data']['components'][1]['price']['amount'],
                                 
$commerce_order->commerce_order_total['und'][0]['currency_code']
                          );

# Get Products total (excl. shipping and packaging) plus currency
$products_total = commerce_currency_format(
                                 
$commerce_order->commerce_order_total['und'][0]['data']['components'][0]['price']['amount'],
                                 
$commerce_order->commerce_order_total['und'][0]['currency_code']
                          );
?>

Bestell-Nummer: [commerce-order:order-number]
E-Mail: [commerce-order:mail]
Administration: [commerce-order:admin-url]
Bestell-IP-Adresse: [commerce-order:hostname]
<?php
echo "\nGesamtwert, ohne Porto & Verpackung: " . $products_total;
echo
"\nPorto- und Verpackung: " . $shipping_total ."\n";
?>

Gesamtwert total: [commerce-order:commerce-order-total:amount_decimal] [commerce-order:commerce-order-total:currency_code]
<?php
echo "\n\nVersand-Adresse";
echo
"\nVorname:\t" . $shipping_address['first_name'];
echo
"\nNachname:\t" . $shipping_address['last_name'];
echo
"\nAdressfeld 1:\t" . $shipping_address['thoroughfare'];
echo
"\nAdressfeld 2:\t" . $shipping_address['premise'];
echo
"\nLand:\t\t" . $shipping_address['country'];
echo
"\nPostleitzahl:\t" . $shipping_address['postal_code'];
echo
"\nOrt:\t\t" . $shipping_address['locality'];

echo
"\n\n\nBestellungs-Details";
foreach(
$line_items as $line_item){
   if(
$line_item->type == 'product'){
     
$product = commerce_product_load($line_item->commerce_product['und'][0]['product_id']);

     
# Formatbeispiel:
      # 2x      BUCH-CCIP         60.00 CHF       Contatto con i Plejaren
      # 2x      BUCH-EQWS       40.00 CHF       Ein Quentchen Wissen, Sinn und Weisheit
     
echo "\n" . number_format($line_item->quantity) . "x\t" . $product->sku . "\t" . commerce_currency_format($product->commerce_price['und']['0']['amount'],$product->commerce_price['und']['0']['currency_code']) . "\t" . $product->title;
   }
}
#print_r($shipping_address);
?>

cameronbprince on January 16, 2013

You can do this without using the PHP filter by creating your own commerce-order tokens. Here's some example code. Note that $order contains all the order data. Use the devel module's dpm() to view the contents of $order and then format and return the data as desired.

<?php
/**
 * Implements hook_token_info_alter().
 */
function MYMODULE_token_info_alter(&$info) {
 
$info['tokens']['commerce-order']['custom-token'] = array(
   
'name' => t('Custom token'),
   
'description' => t('My custom commerce-order token.'),
  );
}

/**
* Implements hook_tokens().
*/
function MYMODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {

 
$url_options = array('absolute' => TRUE);

  if (isset(
$options['language'])) {
   
$url_options['language'] = $options['language'];
   
$language_code = $options['language']->language;
  }
  else {
   
$language_code = NULL;
  }

 
$sanitize = !empty($options['sanitize']);

 
$replacements = array();

  if (
$type == 'commerce-order' && !empty($data['commerce-order'])) {
   
$order = $data['commerce-order'];

    foreach (
$tokens as $name => $original) {
      switch (
$name) {
        case
'custom-token':
         
$replacements[$original] = 'MY OUTPUT';
          break;

      }
    }
  }

  return
$replacements;
}
?>

nicodv on June 28, 2013

in case I don't want to use php, what conditions, will bring me the labels of the products in the shopping cart of the order?
I'm trying with everything available there, but No-luck is my last name in this one :(

Summit on August 9, 2013

Hi,
Trying to get this working with commerce_message but I got these errors:

Notice: Undefined variable: commerce_order in eval() (regel 2 van /var/www/vhosts/gelukshop.nl/httpdocs/modules/php/php.module(80) : eval()'d code).
Notice: Undefined property: EntityValueWrapper::$commerce_customer_shipping in eval() (regel 3 van /modules/php/php.module(80) : eval()'d code).
Notice: Trying to get property of non-object in eval() (regel 3 van /modules/php/php.module(80) : eval()'d code).

Googling I found this answer than https://drupal.org/node/1086328#comment-4186910
but how to define Commerce_order than...

greetings, Martijn

Alif on October 8, 2013

I've never done anonymous checkouts. But, you should be able to easily accomplish this with a rule.

On Drupal's configuration page, go to rules. Add a new rule.
Get owner email notification also costomer get email same time:)
Event -> Completing the checkout process
Action -> Send Mail (under the system actions)

Under replacement patters you can use [commerce-order:mail] for the email address associated with the order. this is for consumer

Action -> Send Mail (under the system actions)
Under replacement patters you can use[site:mail] for the email address associated with the order. this is for site owner

Save you time
Enjoy E commerce