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;
}

[/code]

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>’;
}

[/code]

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 = ‘[email protected]’;
$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.’ <[email protected]>’ . “rn”;

ob_start();

include(“email_header.php”);

?>

<?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”>
<thead>
<tr>
<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>
</tr>
</thead>
<tbody>
<?php echo $order->email_order_items_table( false, true ); ?>
</tbody>
<tfoot>
<?php
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
?><tr>
<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>
</tr><?php
}
}
?>
</tfoot>
</table>

<?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’ ); ?>

<?php

include(“email_footer.php”);

$message = ob_get_contents();

ob_end_clean();

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

}

add_filter(‘wp_mail_content_type’,’set_content_type’);

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

[/code]

Header & Footer include code

email_header.php

[code type=”php”]

<html style=”background:#444″>
<head>

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

</head>
<body>
<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;
border-top-right-radius:5px;”>
<?php echo $email_subject ?>
</div>
</div>

<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 ?>
</h1>

[/code]

email_footer.php

[code type=”php”]

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

<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
</small>
</div>

</div>
</div>
</body>
</html>

[/code]

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!

  • Pingback: terrance()

  • Arash Arzandehnia

    Hi
    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?