Woocommerce - Send Custom Email on Custom Order Status Change

WooCommerce - send custom email on custom order status change

As Xcid's answer indicates, you need to register the email.

In WC 2.2+ I believe you can do this via the following:

add_action( 'woocommerce_order_status_wc-order-confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );

I'd added a filter to WooCommerce 2.3, so when that comes out custom emails will be able to be added to the list of email actions that WooCommerce registers:

// As of WooCommerce 2.3
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' );

WooCommerce: Add Custom order statuses with custom email notifications

There are some mistakes… For multiple order statuses with custom email notifications use the following complete code (removing before all your related code):

// Enable custom statuses for WooCommerce Orders
add_action('init', 'register_custom_order_statuses');
function register_custom_order_statuses() {

register_post_status('wc-shipped ', array(
'label' => __( 'Shipped', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>')
));

register_post_status('wc-readytocollect ', array(
'label' => __( 'Ready to Collect', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Ready to Collect <span class="count">(%s)</span>', 'Ready to Collect <span class="count">(%s)</span>')
));
}

// Add a custom order status to list of WC Order statuses
add_filter('wc_order_statuses', 'add_custom_order_statuses');
function add_custom_order_statuses($order_statuses) {
$new_order_statuses = array();

// add new order status before processing
foreach ($order_statuses as $key => $status) {
$new_order_statuses[$key] = $status;
if ('wc-processing' === $key) {
$new_order_statuses['wc-shipped'] = __('Shipped', 'woocommerce' );
$new_order_statuses['wc-readytocollect'] = __('Ready to Collect', 'woocommerce' );
}
}
return $new_order_statuses;
}

// Adding custom status statuses to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 50, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
$new_actions = array();

// add new order status before processing
foreach ($actions as $key => $action) {
if ('mark_processing' === $key) {
$new_actions['mark_shipped'] = __( 'Change status to shipped', 'woocommerce' );
$new_actions['mark_readytocollect'] = __( 'Change status to Ready to Collect', 'woocommerce' );
}
$new_actions[$key] = $action;
}
return $new_actions;
}

// Add a custom order statuses action button (for orders with "processing" status)
add_filter( 'woocommerce_admin_order_actions', 'add_custom_order_status_actions_button', 100, 2 );
function add_custom_order_status_actions_button( $actions, $order ) {
$allowed_statuses = array( 'on-hold', 'processing', 'pending', 'shipped', 'readytocollect' ); // Define allowed statuses

// Display the button for all orders that have allowed status (as defined in the array)
if ( in_array( $order->get_status(), $allowed_statuses ) ) {

// The key slug defined from status with the name
$action_slugs = array(
'shipped' => __('Shipped', 'woocommerce'),
'readytocollect' => __('Ready to Collect', 'woocommerce')
);

// Loop through custom statuses
foreach ( $action_slugs as $action_slug => $name ) {
// Display "processing" action button
$actions['processing'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
'name' => __( 'Processing', 'woocommerce' ),
'action' => 'processing',
);

// Display "complete" action button
$actions['complete'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
'name' => __( 'Complete', 'woocommerce' ),
'action' => 'complete',
);

// Display custom status action buttons
if( $order->get_status() !== $action_slug ) {
// Set the action button
$actions[$action_slug] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status='.$action_slug.'&order_id='.$order->get_id() ), 'woocommerce-mark-order-status' ),
'name' => $name,
'action' => $action_slug,
);
}
}
}
return $actions;
}

// Set styling for custom order status action button icon and List icon
add_action( 'admin_head', 'add_custom_order_status_actions_button_css' );
function add_custom_order_status_actions_button_css() {
// The key slug defined from status with the icon code
$action_slugs = array(
'shipped' => '\e019',
'readytocollect' => '\e029'
);
?>
<style>
<?php foreach ( $action_slugs as $action_slug => $icon_code ) : ?>
.wc-action-button-<?php echo $action_slug; ?>::after {
font-family: woocommerce !important; content: "<?php echo $icon_code; ?>" !important;
}
<?php endforeach; ?>
</style>
<?php
}

// Adding action for custom statuses
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $action ) {
$actions[] = 'woocommerce_order_status_wc-shipped';
$actions[] = 'woocommerce_order_status_wc-readytocollect';

return $actions;
}

add_action( 'woocommerce_order_status_wc-shipped', array( 'WC_Emails', 'send_transactional_email' ), 10, 1 );
add_action( 'woocommerce_order_status_wc-readytocollect', array( 'WC_Emails', 'send_transactional_email' ), 10, 1 );

// Sending an email notification when order get a custom status
add_action('woocommerce_order_status_shipped', 'cutom_status_trigger_email_notification', 10, 2 );
add_action('woocommerce_order_status_readytocollect', 'cutom_status_trigger_email_notification', 10, 2 );
function cutom_status_trigger_email_notification( $order_id, $order ) {
WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}

// Customize email heading for your custom statuses email notifications
add_filter( 'woocommerce_email_heading_customer_processing_order', 'custom_email_heading_for_custom_order_status', 10, 2 );
function custom_email_heading_for_custom_order_status( $heading, $order ){
// Here your custom statuses slugs / Heading texts pairs
$data = array(
'shipped' => __('Planet Vape {order_number} has been Shipped!','woocommerce'),
'readytocollect' => __('Planet Vape {order_number} is Ready to Collect!','woocommerce')
);

// Loop through each custom status / heading text pairs
foreach ( $data as $custom_status => $heading_text ) {
// Change an email notification heading text when a order get a custom status
if( $order->has_status( $custom_status ) ) {
$email = WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']; // Get the specific WC_emails object

$heading = $email->format_string( $heading_text ); // don't return directly
}
}
return $heading;
}

// Customize email subject for your custom statuses email notifications
add_filter( 'woocommerce_email_subject_customer_processing_order', 'custom_email_subject_for_custom_order_status', 10, 2 );
function custom_email_subject_for_custom_order_status( $subject, $order ){
// Here your custom statuses slugs / Heading texts pairs
$data = array(
'shipped' => __('Planet Vape {order_number} has been Shipped!','woocommerce'),
'readytocollect' => __('Planet Vape {order_number} is Ready to Collect!','woocommerce')
);

// Loop through each custom status / subject text pairs
foreach ( $data as $custom_status => $subject_text ) {
// Change an email notification heading text when a order get a custom status
if( $order->has_status( $custom_status ) ) {
$email = WC()->mailer()->get_emails()['WC_Email_Customer_Processing_Order']; // Get the specific WC_emails object

$subject = $email->format_string( $subject_text ); // don't return directly
}
}
return $subject;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Send an email notification when custom order status changes in WooCommerce

- 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

Trigger email on order custom status change in Woocommerce

Wen using woocommerce_order_status_{$status_transition[to]} composite hook, you just need to remove wc- from the status slug like:

add_action( 'woocommerce_order_status_test-in-progress', array( $this, 'trigger' ), 10, 10 );

And it should work.

Add a new order status that Send an email notification in WooCommerce 4+

Based on this answer code and this unaccepted answer thread, Here is the revisited code, that will add a custom status to WooCommerce orders and will trigger a customized email notification for this custom status:

// register a custom post status 'awaiting-delivery' for Orders
add_action( 'init', 'register_custom_post_status', 20 );
function register_custom_post_status() {
register_post_status( 'wc-awaiting-delivery', array(
'label' => _x( 'Kargoya Verildi', '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( 'Kargoya Verildi <span class="count">(%s)</span>', 'Kargoya Verildi <span class="count">(%s)</span>', 'woocommerce' )
) );
}

// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-awaiting-delivery'] = _x( 'Kargoya Verildi', 'Order status', 'woocommerce' );
return $order_statuses;
}

// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
$actions['mark_awaiting-delivery'] = __( 'Kargoya Verildi', 'woocommerce' );
return $actions;
}

// Adding action for 'awaiting-delivery'
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $actions ) {
$actions[] = 'woocommerce_order_status_wc-awaiting-delivery';
return $actions;
}

add_action( 'woocommerce_order_status_wc-awaiting-delivery', array( WC(), 'send_transactional_email' ), 10, 1 );

// Sending an email notification when order get 'awaiting-delivery' status
add_action('woocommerce_order_status_awaiting-delivery', 'awaiting_delivery_order_status_email_notification', 20, 2);
function awaiting_delivery_order_status_email_notification( $order_id, $order ) {
// HERE below your settings
$heading = __('Kargoya Verildi','woocommerce');
$subject = '[{site_title}] Siparişiniz Kargoya Verildi ({order_number}) - {order_date}';

// The email notification type
$email_key = 'WC_Email_Customer_Processing_Order';

// Get specific WC_emails object
$email_obj = WC()->mailer()->get_emails()[$email_key];

// Sending the customized email
$email_obj->trigger( $order_id );
}

// Customize email heading for this custom status email notification
add_filter( 'woocommerce_email_heading_customer_processing_order', 'email_heading_customer_awaiting_delivery_order', 10, 2 );
function email_heading_customer_awaiting_delivery_order( $heading, $order ){
if( $order->has_status( 'awaiting-delivery' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$heading_txt = __('Kargoya Verildi','woocommerce'); // New heading text

return $email_obj->format_string( $heading_txt );
}
return $heading;
}

// Customize email subject for this custom status email notification
add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_customer_awaiting_delivery_order', 10, 2 );
function email_subject_customer_awaiting_delivery_order( $subject, $order ){
if( $order->has_status( 'awaiting-delivery' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$subject_txt = sprintf( __('[%s] Siparişiniz Kargoya Verildi (%s) - %s', 'woocommerce'), '{site_title}', '{order_number}', '{order_date}' ); // New subject text

return $email_obj->format_string( $subject_txt );
}
return $subject;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.



Related Topics



Leave a reply



Submit