WooCommerce Tracking Code Notifications

Automatically send SMS when shipping gateways add tracking codes to WooCommerce orders.

Automatically notify customers via SMS when shipping gateways add tracking codes to their WooCommerce orders. This reduces support inquiries and keeps customers informed about their shipments.

How It Works

  1. Shipping Gateway processes the shipment and adds an order note containing the tracking code
  2. WSMS listens for new order notes
  3. When a note matches your tracking code pattern, WSMS sends an SMS to the customer

Example Order Note

Shipping gateways typically add notes in this format:

Tracking code: 123456789 (Carrier: DHL)

Implementation

Add this code to your theme’s functions.php or a custom plugin:

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;
    }

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

    // Check if the note contains tracking code
    if (!preg_match('/Tracking code:/i', $note_content)) {
        return;
    }

    // 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
    );
}

Customization

Change the Pattern

Modify the regex pattern to match your shipping gateway’s format:

// For "Shipment ID:" format
if (!preg_match('/Shipment ID:/i', $note_content)) {
    return;
}

// For multiple patterns
if (!preg_match('/Tracking code:|Shipment ID:|AWB:/i', $note_content)) {
    return;
}

Custom Message Format

Send a customized message instead of the raw order note:

// Extract tracking number
preg_match('/Tracking code:\s*(\S+)/i', $note_content, $matches);
$tracking_number = $matches[1] ?? '';

$message = sprintf(
    'Your order #%d has shipped! Track it: %s',
    $order_id,
    $tracking_number
);

wp_sms_send($customer_number, $message);

Compatible Shipping Gateways

This solution works with any shipping gateway that adds order notes, including:

  • OTO
  • DHL
  • FedEx
  • UPS
  • Local courier integrations

Last updated: December 23, 2024