WooCommerce: SMS when an order status changes
A short PHP snippet sends an SMS through SendHarbor when an order becomes processing, completed or cancelled. No marketplace plugin is needed.
WooCommerce webhooks send WooCommerce’s own order data, while SendHarbor expects a message with a phone and a text. Using them would need a relay server in between. The snippet calls the SendHarbor API from your store directly.
What you need
- WordPress with WooCommerce and access to the site files: FTP, SSH or the file manager of your hosting.
- A SendHarbor API key with the right messages:send. A sandbox key is enough for the first test.
- Customer phone numbers in international format, for example +15551234567. Other numbers are skipped with an order note.
No account or key yet? The quick start creates an account, connects your Android phone and shows ready code for this tool with your key. Quick start on your phone
Steps
-
Add the settings to wp-config.php
Add these lines above the line that says to stop editing. Keep the key in this file, not in the database or the theme.
wp-config.php define( 'SENDHARBOR_API_KEY', 'REPLACE_WITH_YOUR_KEY' ); define( 'SENDHARBOR_DEVICE_ID', 'REPLACE_WITH_PHONE_ID' ); define( 'SENDHARBOR_SANDBOX', true ); // false for real SMS -
Create the snippet
Create the file wp-content/mu-plugins/sendharbor-order-sms.php with the code below. Create the mu-plugins folder if it does not exist. WordPress loads files from this folder automatically.
wp-content/mu-plugins/sendharbor-order-sms.php <?php /** * Plugin Name: SendHarbor order SMS * Description: Sends an SMS through SendHarbor when a WooCommerce order changes status. */ defined( 'ABSPATH' ) || exit; // %s is replaced with the order number. Add a line for each status that needs an SMS. const SENDHARBOR_ORDER_TEXTS = array( 'processing' => 'Order %s is confirmed. We will text you when it ships.', 'completed' => 'Order %s has shipped. Thank you for your purchase.', 'cancelled' => 'Order %s was cancelled. Reply to this SMS if this is a mistake.', ); add_action( 'woocommerce_order_status_changed', function ( $order_id, $from, $to ) { if ( array_key_exists( $to, SENDHARBOR_ORDER_TEXTS ) ) { // Action Scheduler ships with WooCommerce and sends in the background. as_enqueue_async_action( 'sendharbor_order_sms', array( $order_id, $to, 1 ), 'sendharbor' ); } }, 10, 3 ); add_action( 'sendharbor_order_sms', function ( $order_id, $status, $attempt ) { $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $phone = preg_replace( '/[\s().-]/', '', (string) $order->get_billing_phone() ); if ( ! preg_match( '/^\+[1-9]\d{6,14}$/', $phone ) ) { $order->add_order_note( 'SendHarbor: the billing phone is not in +15551234567 format, no SMS was queued.' ); return; } $sandbox = defined( 'SENDHARBOR_SANDBOX' ) && SENDHARBOR_SANDBOX; $response = wp_remote_post( 'https://sendharbor.app' . ( $sandbox ? '/api/sandbox/messages' : '/api/messages' ), array( 'timeout' => 15, 'headers' => array( 'Authorization' => 'Bearer ' . SENDHARBOR_API_KEY, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'deviceId' => $sandbox ? 'sandbox-phone' : SENDHARBOR_DEVICE_ID, 'phone' => $phone, 'text' => sprintf( SENDHARBOR_ORDER_TEXTS[ $status ], $order->get_order_number() ), // One key per order and status: a repeat returns the first message, not a second SMS. 'idempotencyKey' => 'woo:' . wp_parse_url( home_url(), PHP_URL_HOST ) . ':' . $order_id . ':' . $status, ) ), ) ); $code = (int) wp_remote_retrieve_response_code( $response ); if ( 202 === $code ) { $body = json_decode( wp_remote_retrieve_body( $response ), true ); $order->add_order_note( 'SendHarbor: SMS queued, message ' . $body['message']['id'] ); return; } $retry = ( is_wp_error( $response ) || 429 === $code || $code >= 500 ) && $attempt < 5; if ( $retry ) { as_schedule_single_action( time() + 300 * $attempt, 'sendharbor_order_sms', array( $order_id, $status, $attempt + 1 ), 'sendharbor' ); } $reason = is_wp_error( $response ) ? $response->get_error_message() : 'HTTP ' . $code; $order->add_order_note( 'SendHarbor: SMS not queued (' . $reason . ')' . ( $retry ? ', will retry.' : '.' ) ); }, 10, 3 );Check: Plugins → Must-Use lists SendHarbor order SMS.
-
Test in the sandbox
Create a test order with your own number and change its status to Processing.
Each send runs in the background. You can see it in WooCommerce → Status → Scheduled Actions, in the group sendharbor.
Check: the order notes say that the SMS is queued, and the message appears in the workspace sandbox.
-
Switch to real SMS
Set SENDHARBOR_SANDBOX to false, set SENDHARBOR_DEVICE_ID to your phone ID and use a key that is not a sandbox key. Repeat the test with a new order.
Check: the SMS arrives and the order note shows the message ID.
The request the snippet sends
{
"deviceId": "DEVICE_ID",
"phone": "+15551234567",
"text": "Order 1042 has shipped. Thank you for your purchase.",
"idempotencyKey": "woo:shop.example:1042:completed"
}
In the sandbox the snippet calls the sandbox address with the phone ID sandbox-phone.
{
"deviceId": "sandbox-phone",
"phone": "+15551234567",
"text": "Order 1042 has shipped. Thank you for your purchase.",
"idempotencyKey": "woo:shop.example:1042:completed"
}
Repeats and errors
- Each order and status has one key. If the order comes back to the same status, SendHarbor returns the first message and sends no second SMS.
- Network errors, 429 and 5xx answers are retried up to five times with growing pauses. Other errors are written to the order notes.
- If you change a text after a send, the same order and status get the answer 409. This protects the customer from a second SMS.
- The snippet reads the order only through WooCommerce order functions, with no direct database queries.