I have created a custom order status in my WooCommerce called Back Order (wc-backorder
):
/**
* Add custom status to order list
*/
add_action( 'init', 'register_custom_post_status', 10 );
function register_custom_post_status() {
register_post_status( 'wc-backorder', array(
'label' => _x( 'Back Order', 'Order status', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Back Order <span class="count">(%s)</span>', 'Back Order <span class="count">(%s)</span>', 'woocommerce' )
) );
}
/**
* Add custom status to order page drop down
*/
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-backorder'] = _x( 'Back Order', 'Order status', 'woocommerce' );
return $order_statuses;
}
Now I want to receive an email whenever an order is received that has been given the status quote. I've created a plugin based on this helpful article: How to Add a Custom WooCommerce Email
This link is containing my plugin source code and the functions.php code.
I added hook in function.php:
add_action( 'woocommerce_order_status_wc-order-confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );
function so_27112461_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_wc-order-confirmed';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'so_27112461_woocommerce_email_actions' );
Nothing happens when an order changed into the 'Back Order' status.
Any ideas?
I've tried loads of different hooks but I can't seem to get the trigger function to run.
I am on latest versions of WordPress and WooCommerce (3.0+)
Thanks
- EDIT / UPDATE -
As the code tutorial you are using is really outdated (2013) for this new mega major version 3.0+, this custom function hooked in woocommerce_order_status_changed
action hook will do the job. So You will be able to send a customized Processing email notification, when order status is changed to your custom status.
Here is that working and tested code for WC 3.0+:
add_action('woocommerce_order_status_changed', 'backorder_status_custom_notification', 10, 4);
function backorder_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
if( $order->has_status( 'backorder' )) {
// Getting all WC_emails objects
$email_notifications = WC()->mailer()->get_emails();
// Customizing Heading and subject In the WC_email processing Order object
$email_notifications['WC_Email_Customer_Processing_Order']->heading = __('Your processing Back order','woocommerce');
$email_notifications['WC_Email_Customer_Processing_Order']->subject = 'Your {site_title} processing Back order receipt from {order_date}';
// Sending the customized email
$email_notifications['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
AS your custom status is wc-backorder
, but not wc-order-confirmed
, you just need to replace everywhere wc-order-confirmed
by wc-backorder
.
To make it work, you will have to change the 2 last hooked functions this way:
add_action( 'woocommerce_order_status_wc-backorder', array( WC(), 'send_transactional_email' ), 10, 1 );
add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_wc-backorder';
return $actions;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work (I can't test it as there is no the code of your custom plugin).
Reference source code: woocommerce_order_status_{$this->status_transition[to]}
action hook
add_action("woocommerce_order_status_changed", "my_custom_notification");
function my_custom_notification($order_id, $checkout=null) {
global $woocommerce;
$order = new WC_Order( $order_id );
if($order->status === 'backorder' ) {
// Create a mailer
$mailer = $woocommerce->mailer();
$message_body = __( 'Hello world!!!' );
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message );
}
}
Try this