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"
Comments
Once you're editing "Send an
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.
Thankyou for Sharing. This was very understandable
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
What information do you need
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.
What information do you need
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.
E-mailing Invoices
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
If you have the PHP filter
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.
Thanks again that would be excellent
"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.
can you access the products
can you access the products from here. I'm struggling to get the individual product data
You can access the products.
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.
This sounds really good
Please show us when ready
Email Order Info
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'];
?>
Thankyou for sharing
Thankyou for sharing and the instructions
There is also a module that
There is also a module that sends invoice when a customer submit an order. Commerce invoice mail
Thank you for posting this
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'];
?>
Oops! Should have looked more
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.
Total price
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
Wrong place
Sorry, I wanted to post tis comment to Prince Manfred's post
any updates on this PHP code
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
tried many replacement patters to get fulfillment order details
The goal: get fulfillment center email with order details so they can process order.
I created rule > action > many replacement patterns to get right information, to no avail.
No luck here either
Also trying to do this. No luck here, either.
My PHP code as an example
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);
?>
Custom tokens/replacement patterns
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;
}
?>
still can't...
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 :(
Hi,
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
Get owner email notification also costomer get email same time:)
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
Hi, see: https://drupal.org
Hi, see: https://drupal.org/project/commerce_message great messages and rules to begin with!
Greetings, Martijn