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.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 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.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 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.

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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 = 'info@domain.nl';
$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.' <no-reply@domein.nl>' . "\r\n";

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

Header & Footer include code

email_header.php

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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>

email_footer.php

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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>

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

rsschouwenaar

About Raymon Schouwenaar

Hi my name is Raymon Schouwenaar and i'm an passionate webdesigner, webdeveloper, blogger. I also love to build and design responsive website's. I like to design beautiful clean web and mobile user interface's. And i like a lot to blog about Responsive Web design & Development, CSS3, HTML5, Wordpress, Plugin. If you want to contact me, i look forward to your message on twitter.

11 thoughts on “Custom Woocommerce email notification for dealer or supplier

  1. 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!

  2. Red De Guzman

    okay disregard my comment. thnks

  3. rsschouwenaar

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

  4. 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 . ‘, some@email.com‘;
        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 some@email.com 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?

  5. .

    thanks!

  6. 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

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *

De volgende HTML-tags en -attributen zijn toegestaan:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>