← All integrations

WooCommerce SMS notifications without a plugin

Customers read texts faster than emails. A short snippet hooks into WooCommerce order statuses and texts the customer when an order is paid (Processing) and when it is completed, from your shop phone number. No SMS plugin, no extra subscription.

Who this is for

  • WooCommerce shops that send up to a few hundred order texts a day.
  • Owners who want customers to see the shop's own number and be able to reply to it.
  • Developers who prefer 60 lines of code to another plugin.

Before you start

  • An Android phone (8.0 or newer) with a SIM card that can send SMS.
  • The NS SMS app from Google Play, set as the default SMS app, with "Accept API requests" turned on in the API tab.
  • Your phone ID (device_id) and API key (nss_...) from the API tab. Below they appear as YOUR_DEVICE_ID and nss_YOUR_API_KEY.
  • Access to wp-config.php and to a child theme (or your own small plugin), by FTP or your hosting file manager. Take a backup first.
  • A phone field at checkout. The snippet uses the billing phone and billing country.

No account, email or password. Install the app, turn on the API and copy your phone ID and API key.

Setup, step by step

  1. Step 1.Add your keys to wp-config.php

    Add the two constants above the line "That's all, stop editing!". Keys in wp-config.php stay out of the database and out of theme files.

    wp-config.php
    define('NS_SMS_API_KEY', 'nss_YOUR_API_KEY');
    define('NS_SMS_DEVICE_ID', 'YOUR_DEVICE_ID');
    // Only for the optional status webhook: the signing secret shown in the app.
    define('NS_SMS_WEBHOOK_SECRET', 'YOUR_SIGNING_SECRET');
  2. Step 2.Add the snippet to functions.php

    Paste the snippet into the functions.php of your child theme (a parent theme update would remove it). The woocommerce_order_status_processing and woocommerce_order_status_completed hooks run when an order moves to that status. The snippet turns the billing phone into E.164, sends the SMS and adds an order note with the result.

    functions.php (child theme)
    <?php
    // NS SMS: text the customer when an order is paid (processing) and when it is completed.
    add_action('woocommerce_order_status_processing', 'ns_sms_order_status', 10, 2);
    add_action('woocommerce_order_status_completed', 'ns_sms_order_status', 10, 2);
    
    function ns_sms_order_status($order_id, $order = null)
    {
        $order = $order instanceof WC_Order ? $order : wc_get_order($order_id);
        if (!$order || !defined('NS_SMS_API_KEY')) {
            return;
        }
    
        $templates = [
            'processing' => 'Thanks for your order #%s at %s. We are preparing it now.',
            'completed'  => 'Good news: your order #%s from %s is on its way.',
        ];
        $status = $order->get_status();
        $to = ns_sms_e164($order->get_billing_phone(), $order->get_billing_country());
        if (!isset($templates[$status]) || $to === '') {
            return;
        }
    
        $response = wp_remote_post('https://sms-api.n-soft.pl/v1/sms/send', [
            'timeout' => 10,
            'headers' => [
                'Authorization'   => 'Bearer ' . NS_SMS_API_KEY,
                'Content-Type'    => 'application/json',
                // The same order and status never sends twice.
                'Idempotency-Key' => 'wc-' . $order->get_id() . '-' . $status,
            ],
            'body' => wp_json_encode([
                'device_id' => NS_SMS_DEVICE_ID,
                'to'        => $to,
                'text'      => sprintf($templates[$status], $order->get_order_number(), get_bloginfo('name')),
            ]),
        ]);
    
        $code = wp_remote_retrieve_response_code($response);
        $order->add_order_note($code === 202
            ? 'SMS sent to ' . $to
            : 'SMS not sent: ' . (is_wp_error($response) ? $response->get_error_message() : wp_remote_retrieve_body($response)));
    }
    
    // "0412 345 678" + "AU" -> "+61412345678". Numbers with + or 00 are kept as they are.
    function ns_sms_e164($phone, $country)
    {
        $phone = preg_replace('/[^\d+]/', '', (string) $phone);
        if ($phone === '') {
            return '';
        }
        if (strpos($phone, '+') === 0) {
            return $phone;
        }
        if (strpos($phone, '00') === 0) {
            return '+' . substr($phone, 2);
        }
        $prefix = WC()->countries->get_country_calling_code($country);
        $prefix = is_array($prefix) ? reset($prefix) : $prefix;
    
        return $prefix ? $prefix . ltrim($phone, '0') : '';
    }
  3. Step 3.Test with your own order

    Place a test order with your own phone number, then change its status to Processing and Completed in WooCommerce > Orders. The order notes show "SMS sent to ..." or the reason it was not sent.

  4. Step 4.Adjust the texts and statuses

    Change the texts in $templates. For another status, add one more add_action line (for example woocommerce_order_status_on-hold) and a template with the same key (on-hold). Keep texts under 160 plain characters to fit one SMS.

Check the status and receive webhooks

The send request answers 202 with a message ID right away. The status then moves through received (the phone has it), sent and delivered, or failed with an error code. You can ask for it with GET /v1/sms/{id}, or let the server POST every change to a webhook address that you set in the app (API tab). Webhooks work on every plan and also report usage.warning, usage.limit_reached, device.offline and device.online.

The order note tells you whether the API accepted the SMS. For the delivery status, add a small REST endpoint to the same functions.php, set https://YOUR-SHOP/wp-json/ns-sms/v1/status as the webhook address in the app and add NS_SMS_WEBHOOK_SECRET (the signing secret shown in the app) to wp-config.php. The endpoint checks the X-NS-Signature header before trusting the event:

functions.php (child theme)
// Webhook address for the app: https://YOUR-SHOP/wp-json/ns-sms/v1/status
add_action('rest_api_init', function () {
    register_rest_route('ns-sms/v1', '/status', [
        'methods'             => 'POST',
        'callback'            => 'ns_sms_webhook',
        'permission_callback' => '__return_true', // the signature is checked below
    ]);
});

function ns_sms_webhook(WP_REST_Request $request)
{
    $body = $request->get_body();
    // X-NS-Signature: t=<unix time>,v1=<hex HMAC-SHA256 of "<t>.<body>">
    parse_str(str_replace(',', '&', (string) $request->get_header('x-ns-signature')), $sig);
    $t = (string) ($sig['t'] ?? '');
    $expected = hash_hmac('sha256', $t . '.' . $body, NS_SMS_WEBHOOK_SECRET);
    if (!hash_equals($expected, (string) ($sig['v1'] ?? '')) || abs(time() - (int) $t) > 300) {
        return new WP_REST_Response(null, 401);
    }

    $event = json_decode($body, true);
    if (($event['event'] ?? '') === 'sms.status' && ($event['sms']['status'] ?? '') === 'failed') {
        error_log('NS SMS failed: ' . $event['sms']['id'] . ' ' . $event['sms']['error_code']);
    }

    return new WP_REST_Response(null, 204);
}

Troubleshooting

  • No order note at all: the hooks did not run. Check that the snippet is in the active theme and that the constants are defined.
  • "SMS not sent" with validation_failed: the billing phone could not be turned into E.164. Customers who type the number with + and the country code always work; the snippet drops one leading 0 for local numbers, which fits most countries but not all (Italy, for example, keeps it).
  • Payment gateways move paid orders to Processing automatically, so that text goes out without anyone clicking. The Idempotency-Key makes sure the same order and status is never texted twice.
  • Saving an order waits for the API for up to 10 seconds. It usually answers in well under a second.
  • 401 unauthorized: the Authorization header must be "Bearer " followed by the key, and device_id must belong to that key.
  • 403 api_disabled: open the app and turn on "Accept API requests" in the API tab.
  • 422 validation_failed: the number must be in international E.164 format, with + and the country code (for example +14155550123), and the text at most 1,600 characters.
  • 429: more than 30 requests a minute, or the daily or monthly cap of your plan (Free 50 a day, Pro 500, Business 1,000). The response says which one.

SMS API reference → SMS error codes → Plans and daily limits →

Use cases for WooCommerce

Other integrations