Send email from WooCommerce when order status is changed to a custom order status
woocommerce
Solution
I believe you can use your custom hook, but first you have to register it. I have a pull request pending with WooThemes to allow the email actions you discovered in core to be filtered. But if/until it is accepted this is how it should be done:
/**
* Register the "woocommerce_order_status_pending_to_quote" hook which is necessary to
* allow automatic email notifications when the order is changed to refunded.
*
* @modified from http://stackoverflow.com/a/26413223/2078474 to remove anonymous function
*/
add_action( 'woocommerce_init', 'so_25353766_register_email' );
function so_25353766_register_email(){
add_action( 'woocommerce_order_status_pending_to_quote', array( WC(), 'send_transactional_email' ), 10, 10 );
}
Update for WooCommerce 2.3.11+
With this commit WooCommerce does allow you to filter the email actions. So, in theory, you should now be able to register an action as an email trigger with the following:
/**
* Register "woocommerce_order_status_pending_to_quote" as an email trigger
*/
add_filter( 'woocommerce_email_actions', 'so_25353766_filter_actions' );
function so_25353766_filter_actions( $actions ){
$actions[] = "woocommerce_order_status_pending_to_quote";
return $actions;
}
Problem
I have created a custom order status in my WooCommerce installation, called Quote. ``` /* * Change order status on new orders depending on order contents: * If any product in the order is availble for quote, return 'quote' status. * Otherwise the order status will be set to processing. */ add_filter ('woocommerce_payment_complete_order_status', 'change_status_to_quote_if_applicable', 10, 2); function change_status_to_quote_if_applicable($order_status, $order_id) { $order = new WC_Order($order_id); $items = $order->get_items(); foreach ($items as $item) { $product = get_product($item['product_id']); if(product_available_for_quote($product)){ return 'quote'; } } return $order_status; } ``` Now I want to recieve an email whenever an order is recieved that has been given the status quote. I've created a plugin based on this helpful article: http://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/ My plugin is basically copied from the article, I've just changed the contents of the email. What I wanted to change is what triggers the email. The plugin in the article has this: ``` // Trigger on new paid orders add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ) ); ``` And I want the email to be triggered when an order gets the status 'quote'. I did this: ``` // Trigger on new quote orders add_action( 'woocommerce_order_status_pending_to_quote', array( $this, 'trigger' ) ); ``` Nothing happens when an order gets the 'quote' status. I've checked class-wc-order.php and specifically the function `update_status`, since that's where the `woocommerce_order_status_.$this->status._to_.$new_status->slug` is fired. I did an error_log to see that the `woocommerce_order_status_pending_to_quote` action exists, and it does. But my trigger function in my plugin is never run. Any ideas? I've tried loads of different hooks but I can't seem to get the trigger function to run. Thanks a lot!