WooCommerce: Send Tracking Info to Customer Using Custom Meta Fields

This post covers the creation of a WooCommerce custom order action.
The action will send a notification to our customer with a predefined message.

For our use case, a shipping tracking info message will be send along with a tracking number.

Adding custom order action

This step will show a new action under the “Order actions” dropdown.


Edit your theme’s function.php with the following snippet:

add_filter( 'woocommerce_order_actions', 'add_send_tracking_info_order_action' );
function add_send_tracking_info_order_action( $actions ) {
    $actions['send_tracking_info'] = __( 'Send tracking info', 'my-textdomain' );
    return $actions;

This won’t do anything for the time being.
We will define what the action does on the next step.

Executing the custom order action

The following snippet checks wether a meta field exists in this order. If it exists and it is not empty, it will send a notification to the customer using the add_order_note command.

add_action( 'woocommerce_order_action_send_tracking_info', 'send_tracking_info_process' );
function send_tracking_info_process( $order ) {
       $tracking_number = $order->get_meta('my_shipping_tracking');
       if($tracking_number != "")
            $message = sprintf( __( 'Your order has shipped and its tracking number is %s. You may see the status here: https://shipping-company.com/track.php?tracking_number=%s .', 'my-textdomain' ), $tracking_number, $tracking_number );
            // add_order_note($message, is_customer_note [0 / 1])
            $order->add_order_note( $message, 1);
Notice the 1 in $order->add_order_note( $message, 1);.
If we remove the 1 ($order->add_order_note( $message );) our text will be saved as a note and won’t be sent as a notification to the customer.

Adding the custom meta field on new orders automatically

The following snippet will add an empty meta field to the order during checkout.

add_action( 'woocommerce_checkout_create_order', 'add_custom_meta_field_for_order', 10, 2 );
function add_custom_meta_field_for_order( $order, $data ) {
           $order->update_meta_data( 'my_shipping_tracking', '' );

Sending the notification

While you are on the order page you may scroll down to find the custom fields box.
If an order has been placed after you inserted the above snippets, a my_shipping_tracking should be shown there with an empty value.
You may update that field’s value with the tracking number your shipping company has given you.
If the order was placed earlier, you won’t find the my_shipping_tracking field and you will have to create one manually.


Once you have that field updated, find the Order actions dropdown, select “Send tracking info” and press the execution button.


When the page updates you should see a new note added under the Order notes section. This note has also been sent to the customer.