问题
In previous versions of Woocommerce, an email notification was sent automatically when an order was changed from pending status to cancelled status (In my case, this happens after an allotted time set in the inventory section of the admin).
In WooCommerce 3.0.8 they have removed this automation and labelled as a fix: https://github.com/woocommerce/woocommerce/blob/master/CHANGELOG.txt
And the pull request is here: https://github.com/woocommerce/woocommerce/pull/15170/files
I'm looking to restore this functionality, but obviously copy/pasting this line back in to the Woocommerce core files isn't a good idea as it will get overwritten when the platform updates.
I know the best method would to be to create a function and hook into the cancelled order action via functions.php but after having a look I'm a bit lost about how to do this. Here is the line which was replaced:
add_action( 'woocommerce_order_status_pending_to_cancelled_notification', array( $this, 'trigger' ), 10, 2 );
How can I restore this old automated functionality?
回答1:
The good new: Using woocommerce_order_status_pending_to_cancelled action hook with a custom function hook in it, solve your problem definitively:
add_action('woocommerce_order_status_pending_to_cancelled', 'cancelled_send_an_email_notification', 10, 2 );
function cancelled_send_an_email_notification( $order_id, $order ){
// Getting all WC_emails objects
$email_notifications = WC()->mailer()->get_emails();
// Sending the email
$email_notifications['WC_Email_Cancelled_Order']->trigger( $order_id );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and perfectly works in WooCommerce 3+ (and 3.1+)
回答2:
I can't comment due to less reputation so I am adding this information here for future reference.
Since version 3.0.9 this issue has been fixed by Woocommerce where the notification is sent to Admin. Link
* Fix - Updated `woocommerce_email_actions` to send email when order status changes from processing to cancelled.
You do not require this code anymore.
来源:https://stackoverflow.com/questions/45732404/send-an-email-notification-when-order-status-change-from-pending-to-cancelled