Custom Woocommerce email notification for dealer or supplier

Written by Raymon Schouwenaar on

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 ‘

’.__(‘Dealer’).’

’;

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 ‘

’;

}

// 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 ‘

’.__(‘Dealer’).’: ‘ . $order->order_custom_fields[‘Dealer’][0] . ‘

’;

}

[/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 = ‘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’ . “rn”;

ob_start();

include(“email_header.php”);

?>

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

billing\_first\_name . ‘ ‘ . $order->billing\_last_name ); ?>

<?php echo $client_message; ?>

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

get\_order\_number() ); ?> (%s’, date\_i18n( ‘c’, strtotime( $order->order\_date ) ), date\_i18n( woocommerce\_date\_format(), strtotime( $order->order_date ) ) ); ?>)

email\_order\_items_table( false, true ); ?> get\_order\_item_totals() ) { $i = 0; foreach ( $totals as $total ) { $i++; ?>
”> ”>

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

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

<?php if ( $order->billing_email ) : ?>

billing\_email; ?>

<?php endif; ?>

<?php if ( $order->billing_phone ) : ?>

billing\_phone; ?>

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

email_header.php

[code type=”php”]

<?php echo $email_subject ?>

[/code] **email_footer.php** [code type=”php”]

Warm Regards,

You have received this email because you are a member of . If you would like to stop receiving emails from us, feel free to unregister from our mailing list

[/code]

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


Readmore

What is the JavaScript Set in ES2015 and when should you use it?

The ES2015 spec is full of new features that continuously be introduced in all the new browsers. The Set collection is one of those new things.

My first thought was, well I could just use a normal Array! But I will show you what you can do with the Set collection.

CSS Grid Layout Metro design blocks

The times have come that we don’t have to fiddle around with Floats and Tables anymore! Flexbox is already an established term in the industry of Frontend Developers.