'Woocommerce complete order and add info to email programmatically

I am trying to make an order Completed manually and add tracking info to the email before it has been sent to the customer. I added a note to the order successfully but note is not in the email. How I can add the tracking code to the email?

Here is my code:

if(!empty(wc_get_order($ord->orderId))){
    $order = wc_get_order($ord->orderId);

    if($ord->statusId == 2){ //completed
        $order->add_order_note( 'Tracking Number:'.$ord->trackingNumber );
        $order->update_status( 'completed', $ord->trackingNumber );
    }
    else if($ord->statusId == 3){
        $order->update_status( 'cancelled' );
    }
    var_dump($order);
}


Solution 1:[1]

You can override emails default HTML template by copying /woocommerce/templates/ [+any file you would like to override] to your theme/child theme folder. If you never overrode any woocommerce template in your theme - go here.

The html generated for the completed email is emails/customer-completed-order.php file, add your code to include the tracking number or any text you would like.

EDIT: To trigger your function when you change order status from processing to completed is the following add_action('woocommerce_order_status_completed', 'your_function_name', 10, 1);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1