Send an email notification when custom order status changes in WooCommerce

ε祈祈猫儿з 提交于 2019-12-01 01:55:44

- 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!