Automatically Send SMS Notifications for Tracking Codes Added by Shipping Gateways in WooCommerce

Updated on Jun 15, 2025

Providing customers with timely, accurate shipping updates boosts satisfaction and cuts down on support inquiries. WooCommerce shipping gateways (for example via OTO) automatically add tracking numbers as order notes, such as:

Tracking code: 123456789 (Carrier: DHL)

Manually notifying customers every time a new tracking code appears can quickly become tedious and error-prone. This guide shows you how to use the WP SMS plugin to send an SMS automatically whenever a shipping gateway adds a tracking number to a WooCommerce order.

How It Works

  1. Shipping Gateway processes the shipment and adds an order note in WooCommerce containing the tracking code.
  2. WP SMS listens for new order notes.
  3. When a note matches your “tracking code” pattern, WP SMS sends an SMS to the customer’s phone number on file.

Automating SMS Notifications

Place the following code snippet into your WordPress theme’s functions.php:

add_action('wp_insert_comment', 'wp_sms_handle_new_order_note', 10, 2);

function wp_sms_handle_new_order_note($comment_id, $comment_object) {
    // Check if the comment is an order note
    if ($comment_object->comment_type !== 'order_note') {
        return; // Skip if it's not an order note
    }

    // Get the note content
    $note_content = $comment_object->comment_content;

    // Check if the note contains the specific term (e.g., "Tracking code:")
    if (!preg_match('/Tracking code:/i', $note_content)) {
        return; // Skip if the note doesn't meet the condition. You may change it according to your shipping gateway.
    }

    // Get the order ID from the comment
    $order_id = $comment_object->comment_post_ID;

    // Get the customer's phone number from order
    $customer_number = \WP_SMS\Helper::getWooCommerceCustomerNumberByOrderId($order_id);

    // Check if a valid phone number exists
    if (empty($customer_number)) {
        return;
    }

    wp_sms_send(
        $customer_number,
        $note_content
    );
}

With WP SMS handling your shipping notifications, you can focus on growing your store while your customers stay informed every step of the way.