问题
Is it possible to trigger an outgoing email based on an added order note within WooCommerce?
We've integrated with a stock control system (Mintsoft) who basically send us back the tracking ID via order notes (all linked up via the REST API)
I've managed to hook into the content of the note based on the text inside, as the order object contains pretty much everything you could want - however it's out of scope at the point the usual "completed" mail goes out, meaning a template change is out of the question.
My idea was to disable the automatic email based on status and attempt my own, is there a hook for this?
回答1:
If you have a look to the WC_Order add_order_note() method code, you will see inside it two available hooks, and you can use the first convenient one.
In the code below, you have all arguments data, the order Id, the WC_Order Object and the way to send an email notification:
add_filter( 'woocommerce_new_order_note_data', 'filter_woocommerce_new_order_note_data', 10, 2 );
function filter_woocommerce_new_order_note_data( $args, $args2 ) {
if( ! $args2['is_customer_note'] ){
// Get an instance of the WC_Order Object
$order = wc_get_order( $args2['order_id'] );
// Getting all WC_emails objects
$mailer = WC()->mailer()->get_emails();
// Send the "Completed" notification
$mailer['WC_Email_Customer_Completed_Order']->trigger( $args2['order_id'] );
}
return $args;
}
Code goes on function.php file of your active child theme (or active theme). Tested, it should work.
Related: Add the Shop Manager username to Woocommerce Admin Order notes
来源:https://stackoverflow.com/questions/55263397/trigger-woocommerce-outgoing-email-based-on-an-order-note