WooCommerce order updates by SMS: calling our SMS API when an order changes status

Online Stores·· 1 view· 3 min read

The short answer: WooCommerce tells your code whenever an order changes status, through the woocommerce_order_status_changed action. Hook a function onto that action that calls our SMS API with the customer's billing phone and a text for each status.

Which statuses to notify

WooCommerce status When it happens Example SMS
processing Payment received, order being prepared We have received your order 4521. We will let you know when it ships.
completed Order fulfilled or delivered Your order 4521 is on its way. Thank you!
on-hold Waiting for a bank transfer or reference payment Order 4521 is awaiting payment. The details are in the e-mail we sent you.

What each status means is explained in WooCommerce order statuses explained. Do not notify them all: two or three moments are enough.

The code

Define the key in wp-config.php, away from your theme code, with define('RLX_SMS_CHAVE', 'prod_...'); and add:

// in a plugin of your own or the child theme functions.php
add_action('woocommerce_order_status_changed', function ($id, $de, $para, $order) {
$textos = [
'processing' => 'We have received your order %s. We will let you know when it ships.',
'completed' => 'Your order %s is on its way. Thank you!',
];
if (empty($textos[$para]) || !$order->get_billing_phone()) { return; }
wp_remote_post('https://api.mozout.com/v1/sms/messages', [
'timeout' => 10,
'headers' => [
'Authorization' => 'Bearer ' . RLX_SMS_CHAVE,
'Content-Type' => 'application/json',
'Idempotency-Key' => 'encomenda-' . $id . '-' . $para,
],
'body' => wp_json_encode([
'number' => $order->get_billing_phone(),
'text' => sprintf($textos[$para], $order->get_order_number()),
]),
]);
}, 10, 4);

1 The key is created on the API page in My MozOut, with only sms:write.
2 The phone is the order's billing phone. Ask for it at checkout in the right format: our API accepts numbers with or without the country code, because the system can tell the country from the first digit: 841234567 goes to Mozambique and 923456789 to Angola. So +258 84 123 4567 and 258841234567 are the same number as 841234567.
3 The Idempotency-Key combines the order id and the status: the same change never produces two SMS.
4 The 10-second timeout stops a network problem from slowing down the customer's or the admin's page.
Keep an eye on characters. In Portuguese, letters such as "ã" or "ç" switch the message to Unicode and each part holds fewer characters; in English this rarely happens. Check the cost in how many credits an SMS uses.
Test with a sandbox_ key and a test order: the request is checked but not sent or charged. Switch to the prod_ key once everything is right.

Want to text your WooCommerce customers? See the API page and create the key.

See the API page

SEE ALSO

WooCommerce order statuses explained: pending, on hold, processing and completed

Transactional SMS: order notices, confirmations and alerts your customers expect

Sending SMS through the API: key, permissions, Sandbox and your first request

How many credits an SMS uses: 160, 153, 70 and 67 characters explained

Connecting an ERP, CRM or in-house system to SMS notices: what your developer needs

An API key or token has leaked: what to do, in the right order

RECOMMENDED PRODUCT

WordPress hosting

One-click install, updates handled, and speed that holds up. from $6.60/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.