I have the following code to try to send an order notification email to the customer in WooCommerce.
I have some other code that already made the custom order status "reminder". I can also select this new "reminder" from the Bulk actions dropdown box in the orders page in WooCommerce.
If a select an order and select custom order status "reminder" from the dropdown box it successfully adds a note on the order on that order's page, but it does not send a notification email to the user.
How come?
// Do the bulk action from the selected orders
add_filter('handle_bulk_actions-edit-shop_order', 'status_bulk_action_edit_shop_order', 10, 3);
function status_bulk_action_edit_shop_order($redirect_to, $action, $post_ids) {
if ($action === 'reminder') {
foreach ($post_ids as $post_id) {
$order = wc_get_order($post_id);
$the_order_id = $order->get_id();
$note = 'Test1';
$order->update_status('reminder');
send_order_note_email($the_order_id, $note);
}
}
return $redirect_to;
}
function send_order_note_email($order_id, $note)
{
// Get the order object
$order = wc_get_order($order_id);
// Check if order exists and is a valid order
if (!$order) {
return;
}
// Add the note to the order
$order->add_order_note($note);
// Send the email notification
$mailer = WC()->mailer()->get_emails()['WC_Email_Note_Customer'];
if ($mailer) {
$mailer->trigger($order_id, $note);
}
}
Tried various functions and different hooks.
You are making things much more complicated:
The
WC_Ordermethodadd_order_note()has an additional argument$is_customer_notethat need to bet set to1ortrue, and send automatically thecustomer_noteemail notification by itself.You can use 2 approaches:
Code goes in functions.php file of your child theme (or in a plugin). Both ways should work.