Custom Woocommerce email notification for dealer or supplier

Since a couple of days i was searching of a solution to make my own custom email notification for Woocommerce. And especially to send an dealer or supplier an email that he have got an order to handle.

But Woocommerce has a documentation. But custom stuff like this you wouldn’t find there. So i started myself! And came with an solution.

For my locations i use the WordPress plugin SimpleMap, it’s a nice plugin to create dealer location libary where visitors can search if there near by an dealer.

The code below is all in my functions.php, so if you’re familiar with PHP than you can make some custom stuff out of my code.

Get an custom checkout field with the locations

The function “dealer_checkout_field()” adds an custom dealer checkout field. It’s an selectbox. It’s added like Woocommerce told in there documentation to do it. It’s very simple!

The “get_locations_list()” function gets an array of all published locations (from the SimpleMaps plugin). And outputs the email of the location as an value and the location name as the name that show up in the select.

[code type=”php”]

* Add the field to the checkout
add_action(‘woocommerce_before_order_notes’, ‘dealer_checkout_field’);

function dealer_checkout_field( $checkout ) {

echo ‘<div id=”my_custom_checkout_field”><h2>’.__(‘Dealer’).'</h2>’;

woocommerce_form_field( ‘order_dealer’, array(
‘type’ => ‘select’,
‘class’ => array(‘my-field-class form-row-wide’),
‘label’ => __(‘Kies uw dealer’),
‘placeholder’ => __(‘Select’),
‘options’ => get_locations_list()
), $checkout->get_value( ‘order_dealer’ ));

echo ‘</div>’;


// Get an list of all locations for the email
function get_locations_list() {
$args = array(
‘orderby’ => ‘title’,
‘order’ => ‘DESC’,
‘post_type’ => ‘sm-location’,
‘post_status’ => ‘publish’,
$locations_array = get_posts( $args );
$posts = array();
foreach ($locations_array as $post) {
$posts[get_post_meta($post->ID, ‘location_email’, true)] = $post->post_title;
$posts[] = ‘Select an dealer’;
return $posts;


Save the custom checkout field

The functions below will post and save the custom field so we can use it in the email notification.

[code type=”php”]

* Process the checkout
add_action(‘woocommerce_checkout_process’, ‘my_custom_checkout_field_process’);

function my_custom_checkout_field_process() {
global $woocommerce;

// Check if set, if its not set add an error.
if (!$_POST[‘order_dealer’])
$woocommerce->add_error( __(‘Please enter something into this new shiny field.’) );

* Update the order meta with field value
add_action(‘woocommerce_checkout_update_order_meta’, ‘my_custom_checkout_field_update_order_meta’);

function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST[‘order_dealer’]) update_post_meta( $order_id, ‘Dealer’, esc_attr($_POST[‘order_dealer’]));

* Display field value on the order edition page
add_action( ‘woocommerce_admin_order_data_after_billing_address’, ‘my_custom_checkout_field_display_admin_order_meta’, 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
echo ‘<p><strong>’.__(‘Dealer’).’:</strong> ‘ . $order->order_custom_fields[‘Dealer’][0] . ‘</p>’;


Send that notification to the dealer

With the “woocommerce_checkout_order_processed” action we trigger the function that sends the email. The action will be triggerd when there is an new order.

If you don’t understand the way WordPress handle there email function? Read the SmashingMagazine article about the wp_mail function.

[code type=”php”]

// Triggers for this email
add_action(“woocommerce_checkout_order_processed”, “my_awesome_publication_notification”);

function my_awesome_publication_notification($order_id, $checkout) {
global $woocommerce;

$checkout = $woocommerce->checkout();
$order = new WC_Order( $order_id );

$order = new WC_Order( $order_id );
$checkout = $woocommerce->checkout();

$dealer_email = $checkout->get_value( ‘order_dealer’ );

$author_email = $dealer_email;
if($author_email == ”) {
$author_email = ‘’;
$client_message = __(‘Dealer is not chosen. Contact client for dealer.’,’domain’);

$site_title = __(‘Site name’,’bewe-theme’);

$email_subject = __(‘New order from: ‘.$site_title.”, ‘domain’);

$headers = ‘From:’.$site_title.’ <>’ . “rn”;




<?php do_action( ‘woocommerce_email_header’, $email_heading ); ?>

<p style=”color: #333333;”><?php printf( __( ‘You have received an order from %s. Their order is as follows:’, ‘woocommerce’ ), $order->billing_first_name . ‘ ‘ . $order->billing_last_name ); ?></p>
<?php echo $client_message; ?>

<?php do_action( ‘woocommerce_email_before_order_table’, $order, true ); ?>

<h2 style=”color: #333333;”><?php printf( __( ‘Order: %s’, ‘woocommerce’), $order->get_order_number() ); ?> (<?php printf( ‘<time datetime=”%s”>%s</time>’, date_i18n( ‘c’, strtotime( $order->order_date ) ), date_i18n( woocommerce_date_format(), strtotime( $order->order_date ) ) ); ?>)</h2>

<table cellspacing=”0″ cellpadding=”6″ style=”width: 100%; border: 1px solid #eee;” border=”1″ bordercolor=”#eee”>
<th scope=”col” style=”text-align:left; border: 1px solid #eee;color: #333333;”><?php _e( ‘Product’, ‘woocommerce’ ); ?></th>
<th scope=”col” style=”text-align:left; border: 1px solid #eee;color: #333333;”><?php _e( ‘Quantity’, ‘woocommerce’ ); ?></th>
<th scope=”col” style=”text-align:left; border: 1px solid #eee;color: #333333;”><?php _e( ‘Price’, ‘woocommerce’ ); ?></th>
<?php echo $order->email_order_items_table( false, true ); ?>
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
<th scope=”row” colspan=”2″ style=”color: #333333;text-align:left; border: 1px solid #eee; <?php if ( $i == 1 ) echo ‘border-top-width: 4px;’; ?>”><?php echo $total[‘label’]; ?></th>
<td style=”text-align:left; color: #333333;border: 1px solid #eee; <?php if ( $i == 1 ) echo ‘border-top-width: 4px;’; ?>”><?php echo $total[‘value’]; ?></td>

<?php do_action(‘woocommerce_email_after_order_table’, $order, true); ?>

<?php do_action( ‘woocommerce_email_order_meta’, $order, true ); ?>

<h2 style=”color: #333333;”><?php _e( ‘Customer details’, ‘woocommerce’ ); ?></h2>

<?php if ( $order->billing_email ) : ?>
<p style=”color: #333333;”><strong style=”color: #333333;”><?php _e( ‘Email:’, ‘woocommerce’ ); ?></strong> <?php echo $order->billing_email; ?></p>
<?php endif; ?>
<?php if ( $order->billing_phone ) : ?>
<p style=”color: #333333;”><strong style=”color: #333333;”><?php _e( ‘Tel:’, ‘woocommerce’ ); ?></strong> <?php echo $order->billing_phone; ?></p>
<?php endif; ?>

<?php woocommerce_get_template( ’emails/email-addresses.php’, array( ‘order’ => $order ) ); ?>

<?php do_action( ‘woocommerce_email_footer’ ); ?>



$message = ob_get_contents();


// wp_mail($author_email, $email_subject, $message);
wp_mail($author_email, $email_subject, $message, $headers);



function set_content_type($content_type){
return ‘text/html’;


Header & Footer include code


[code type=”php”]

<html style=”background:#444″>

<title><?php echo $email_subject ?></title>

<div id=”email_container”>
<div style=”width:570px; padding:0 0 0 20px; margin:50px auto 12px auto” id=”email_header”>
<span style=”background:#ad0e24; color:#fff; padding:12px;font-family:trebuchet ms; letter-spacing:1px;
-moz-border-radius-topleft:5px; -webkit-border-top-left-radius:5px;
border-top-left-radius:5px;moz-border-radius-topright:5px; -webkit-border-top-right-radius:5px;
<?php echo $email_subject ?>

<div style=”width:550px; padding:0 20px 20px 20px; background:#fff; margin:0 auto; border:2px #950c1f solid;
moz-border-radius:5px; -webkit-border-radus:5px; border-radius:5px; color:#333333;line-height:1.5em; ” id=”email_content”>

<h1 style=”padding:5px 0 0 0; font-family:georgia;font-weight:500;font-size:24px;color:#950c1f;padding-bottom:10px;border-bottom:1px solid #950c1f”>
<?php echo $email_subject ?>



[code type=”php”]

<p style=”color: #333333;”>
Warm Regards,<br>
<?php echo $site_title; ?>

<div style=”text-align:center; border-top:1px solid #eee;padding:5px 0 0 0;” id=”email_footer”>
<small style=”font-size:11px; color:#999; line-height:14px;”>
You have received this email because you are a member of <?php echo $site_title; ?>.
If you would like to stop receiving emails from us, feel free to
unregister from our mailing list



I hope this code and blog are usefull for you! If you like it, share it 🙂 Thanks!

  • Red De Guzman

    Hi Raymon! Nice post! Got problems integrating it though. I’m don’t see the additional checkbox in the checkout. I’m sure I followed your instructions. I pasted the code for functions.php and created email_header.php and email_footer.php and pasted the codes for each. How do I get this work? thanks!

  • Red De Guzman

    okay disregard my comment. thnks

  • rsschouwenaar

    Hi @reddeguzman:disqus do you need the code for the selectbox in the checkout page? Like to help you!

  • Brian Hood

    If I have a plugin that creates custom meta data for users (for example – a buyer’s regional manager’s email address) would this approach be able to trigger an email to the buyer’s manager when woocommerce sends the confirmation order email to the buyer?
    In other words, if I store a second email address for my buyers could this script send emails to the second email when woocommerce generates a new order email.
    If it does, you’re brilliant. There are many people looking for this feature in woocommerce.
    I’ll give it a try, but my PHP skills are not that strong.
    Thanks for the insight.

    • rsschouwenaar

      I think that it is posible to create meta data for an user role, and get this infomation in an seletbox in the woocommerce checkout. Because you can create an array of all users by it’s role!

      But how how i build the solution above was just different. I created many locations for the dealer/supplier and there was the information about the dealer stored. So i created an selectbox in the checkout where an buyer can choose his dealer. When the user submitted the order, the dealer will get also an email with all the information of the buyer and order.

      Can you explane me what the solution is that you need? Maybe i could help you on you’re way. And honestly my skills with PHP are not the best of the best. But developing WordPress websites and plugins give me an better understanding of PHP. So WordPress helpt me learning PHP.

      So if i can help you @disqus_AssTy8nIQb:disqus lett me know!

      • Brian Hood

        Thanks for the reply. To answer your question: I have a woocommerce install which I plan to use as an ordering site for select customers. When a customer places an order, the order needs to be approved by the customer’s Regional Manager (RM). I am trying to get woocommerce to automatically BCC a copy of the Order notification email generated by woocommerce to the customer’s RM for approval. After the order has been approved by the RM it will be processed.

        I created a custom field to store the RM email for each customer in the database and added a custom field to display the RM email the WC checkout page so the customer can verify or add the email address during checkout. I can get WC to email an extra recipient this way:

        function woo_extra_email_recipient($recipient, $object) {
        $recipient = $recipient . ‘,‘;
        return $recipient;
        add_filter( ‘woocommerce_email_recipient_customer_completed_order’, ‘woo_extra_email_recipient’, 10, 2);

        So I think I am close to what I want. The problem I haven’t figured out yet is how to replace the above with the email address stored in the the custom field rm_email I created. Your script loads the value from the map plugin into a array to let the user select the recipient. I need to load the extra email recipient’s address from the user’s meta data (stored in the new custom field for the user).

        Once I figure that out I’ll probably create a new email template based on your design to notify the RM one of his employees placed an order.

        Any tips will be much appreciated. Thanks again.

        • rsschouwenaar

          @disqus_AssTy8nIQb:disqus So if i understand you right. You want an select in the checkout page like in my code. But you want after an order to send an email to the RM. So in the select you want alle the email of the RM’s, so an customer can select his RM? I like to know if i’m right, so i can better help you 🙂

          • Brian Hood

            Loading all the email addresses for the RM from the custom field I created into an array is a valid approach, but it might create an error by the user so my preferred method is to load the RM address automatically from the database.

            For each customer/buyer I created the custom field – RM_email – to enter their respective RM address in the database. To clarify further, each customer has a required RM address in their metadata; each customer can only have 1 RM although an RM address can be used for multiple customers. I’m more of a CSS/HTMLguy, but I used to write a lot of SQL, and in SQL terms the relationships are:
            Cust to RM -> 1 (each customer is assigned 1 RM)

            RM to Cust -> 1 to Many (each RM can have multiple Customers assigned)

            Sorry if that’s redundant, I just want to clarify the data relationships.

            So yes, your array method would work as it allows the Cust to chose his RM from a list, but for accuracy it might be better to automatically get the assigned customer’s RM already stored in the database.

            Then, once the customer places the order, I want WC to send an email to the RM to notify them the order has been placed so it can be approved. Importantly, that email is in addition to the standard order confirmation WC send the customer. I think the code I sent previously will do that once I create the new email template point and point the ‘woo_extra_email_recipient’ function I created to the new template.

            That’s where I’m stuck. I can load variables and assign information to them in pure/raw PHP on a testing server, but translating my logic into WP/WC is giving me fits.

            Sorry to be long-winded, just trying to explain the issue.

          • rsschouwenaar

            Sorry for my late reaction! But i was very busy with a project!

            I think i understand wat you want. But how do you want to automate the proces, that a client get an specific RM? If you know how to figure that out? Than you can make a hidden field in the checkout, where the email adress of the RM is loaded. And than you can use a bit of my code to send an email to the RM.

            Is that something that could help you?

  • Pingback: terrance()

  • Arash Arzandehnia

    I want to Send a Private Serial Number for each customer who checked out the product , this number must be unique for each user in each check out , and i want to send this number with woocommerce email and sms plugin to customers after they successfully checked out.

    how can i do it ?
    Thank you

  • Yelitza Cintrón

    Raymon!! You are the best!!! Exactly what I was looking for!! I’m a little confused… the functions.php you are referring to, is the ones in the WooCommerce plugin or my templates functions.php page?? If it is the ones in WooCommerce plugin, there are a lot of functions.php pages… which of them??? I tried the wc-core-functions.php and the wc-cart-functions.php, but no results!! My main website just went blank!! 🙁

    If I added the three first codes to these two files I got a blank page, if I added to the core one, the whole system went blank, and only in the cart php, the dashboard was ok, but the mail website, the front end became blank and not accesible… PLEASE HELP MEEEEE!! I beg you! 😀

    Hope you can see this soon!!!!

    • Yelitza Cintrón

      Update, I put it on my template functions, and also got the system blank! 🙁

      • Hi Yelitza, hopefully this will help a bit. I know you posted a while ago, but just in case you still need help. The code should be added to a functions.php file that is associated with a child theme. If you update the code within any other functions.php file on your site, then you run the risk of the new code disappearing whenever you update your website/plugins. The child theme’s functions.php ensures that you can update your site and not have your custom code affected or overwritten.
        The reasons for the blank page when you add the code may be due to where you’re adding the new code. Try using this code within a child theme of your theme and see if it works.

  • Yelitza Cintrón

    Hii! Any news?

  • amitasthana

    Hey, Thanks for this. Do you know how one can send a sms to a particular vendor when a order is placed on his product using wc vendors plugin and apg sms plugin in wordpress.

    • @amitasthana:disqus I think you should search for an SMS service, with an special api. So when a order is placed, you can send a post request to that service. Hope that will help 🙂

  • Vladimir Martinovski

    Hi, thanks for this. It is very very close to what I need. Can you point me how to make static 4-5 distributor radio-select field. Dont need SimpleMap plugin. Dealers are fixed. Where and how to change your code to accomplish this.

    • @vladimirmartinovski:disqus i’m sorry, but It is a long time ago that I build this. Hopefully, you can build it without my help. Good luck!

  • Hi Raymon. I am wondering if you would recommend this method still over using the WooCommerce REST API to build a solution?

  • Tobi X

    you put this into functions.php of the theme?