Search code examples
phpwordpresswoocommerceordersemail-notifications

Can't send customer note email notification in WooCommerce


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.


Solution

  • You are making things much more complicated:
    The WC_Order method add_order_note() has an additional argument $is_customer_note that need to bet set to 1 or true, and send automatically the customer_note email notification by itself.

    You can use 2 approaches:

    1. Add a note to customer when order status get changed to "reminder" (the cleanest way and complete way):
    // 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);
                $order->update_status('reminder');
            }
        }
        return $redirect_to;
    }
    
    // Add a note to customer when order get "reminder" status (sends the email notification)
    add_action('woocommerce_order_status_reminder', 'add_reminder_order_note_to_customer', 10, 2);
    function add_reminder_order_note_to_customer( $order_id, $order )
    {
        // Define the customer note to be sent
        $note_to_customer = 'Test1';
    
        // Add the customer note to the order
        $order->add_order_note($note_to_customer, 1);
    }
    
    1. Add a note to customer when changing order status to "reminder" via bulk actions:
    // 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);
                $order->update_status('reminder');
    
                // Define the customer note to be sent
                $note_to_customer = 'Test1';
    
                // Add the customer note to the order
                $order->add_order_note($note_to_customer, 1);
            }
        }
        return $redirect_to;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Both ways should work.