WooCommerce Integration
Enhance your WordPress eCommerce store with automated SMS notifications. Keep customers informed about their orders from purchase to delivery with real-time SMS updates.
Use Case: Improve customer satisfaction and reduce cart abandonment with timely SMS notifications for your WooCommerce store.
How It Works
WooCommerce hooks trigger SMS notifications for:
- New order placement
- Payment confirmation
- Order status changes
- Shipping updates
- Order completion
- Refund processing
- Low stock alerts for customers
- Abandoned cart recovery
Prerequisites
Before you begin, ensure you have:
- An active SMSLeopard account with API credentials (Get started here)
- WordPress website with WooCommerce installed
- PHP 7.4 or higher
- Access to WordPress admin panel
Integration Methods
Method 1: Custom Plugin (Recommended)
Create a custom WordPress plugin for seamless integration:
<?php
/**
* Plugin Name: SMSLeopard for WooCommerce
* Description: Send SMS notifications for WooCommerce orders
* Version: 1.0.0
* Author: Your Name
*/
if (!defined('ABSPATH')) {
exit;
}
class SMSLeopard_WooCommerce {
private $api_key;
private $api_secret;
private $sender_id;
public function __construct() {
// Load settings
$this->api_key = get_option('smsleopard_api_key');
$this->api_secret = get_option('smsleopard_api_secret');
$this->sender_id = get_option('smsleopard_sender_id');
// Hook into WooCommerce order actions
add_action('woocommerce_new_order', [$this, 'send_new_order_sms']);
add_action('woocommerce_order_status_processing', [$this, 'send_processing_sms']);
add_action('woocommerce_order_status_completed', [$this, 'send_completed_sms']);
add_action('woocommerce_order_status_cancelled', [$this, 'send_cancelled_sms']);
add_action('woocommerce_order_status_refunded', [$this, 'send_refunded_sms']);
// Add phone field to checkout
add_filter('woocommerce_checkout_fields', [$this, 'add_phone_field']);
// Admin menu
add_action('admin_menu', [$this, 'add_admin_menu']);
}
public function add_phone_field($fields) {
$fields['billing']['billing_phone']['required'] = true;
$fields['billing']['billing_phone']['priority'] = 25;
return $fields;
}
public function send_new_order_sms($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
if (!$phone) {
return;
}
$order_total = $order->get_total();
$currency = $order->get_currency();
$message = sprintf(
'Thank you for your order #%s! Total: %s %s. We will process it shortly.',
$order->get_order_number(),
$currency,
number_format($order_total, 2)
);
$this->send_sms($phone, $message, $order_id);
}
public function send_processing_sms($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
if (!$phone) {
return;
}
$message = sprintf(
'Great news! Your order #%s is being processed. We\'ll notify you when it ships.',
$order->get_order_number()
);
$this->send_sms($phone, $message, $order_id);
}
public function send_completed_sms($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
if (!$phone) {
return;
}
$customer_name = $order->get_billing_first_name();
$message = sprintf(
'Hi %s! Your order #%s has been completed. Thank you for shopping with us!',
$customer_name,
$order->get_order_number()
);
$this->send_sms($phone, $message, $order_id);
}
public function send_cancelled_sms($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
if (!$phone) {
return;
}
$message = sprintf(
'Your order #%s has been cancelled. If you have questions, please contact our support.',
$order->get_order_number()
);
$this->send_sms($phone, $message, $order_id);
}
public function send_refunded_sms($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
if (!$phone) {
return;
}
$message = sprintf(
'Your order #%s has been refunded. You will receive your refund within 3-5 business days.',
$order->get_order_number()
);
$this->send_sms($phone, $message, $order_id);
}
private function send_sms($phone, $message, $order_id = null) {
if (empty($this->api_key) || empty($this->api_secret)) {
error_log('SMSLeopard: API credentials not configured');
return false;
}
$formatted_phone = $this->format_phone_number($phone);
$data = [
'source' => $this->sender_id,
'destination' => [$formatted_phone],
'message' => $message
];
$response = wp_remote_post('https://api.smsleopard.com/v1/sms/send', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->api_key . ':' . $this->api_secret
],
'body' => json_encode($data),
'timeout' => 15
]);
if (is_wp_error($response)) {
error_log('SMSLeopard Error: ' . $response->get_error_message());
return false;
}
$body = json_decode(wp_remote_retrieve_body($response), true);
// Log SMS sent
if ($order_id) {
add_post_meta($order_id, '_sms_sent', [
'phone' => $formatted_phone,
'message' => $message,
'response' => $body,
'timestamp' => current_time('mysql')
]);
}
return true;
}
private function format_phone_number($phone) {
// Remove all non-numeric characters
$phone = preg_replace('/[^0-9]/', '', $phone);
// Add country code (Kenya example)
if (substr($phone, 0, 1) === '0') {
return '254' . substr($phone, 1);
} elseif (substr($phone, 0, 3) === '254') {
return $phone;
}
return $phone;
}
public function add_admin_menu() {
add_submenu_page(
'woocommerce',
'SMSLeopard Settings',
'SMS Settings',
'manage_options',
'smsleopard-settings',
[$this, 'settings_page']
);
}
public function settings_page() {
if (isset($_POST['smsleopard_save_settings'])) {
update_option('smsleopard_api_key', sanitize_text_field($_POST['api_key']));
update_option('smsleopard_api_secret', sanitize_text_field($_POST['api_secret']));
update_option('smsleopard_sender_id', sanitize_text_field($_POST['sender_id']));
echo '<div class="notice notice-success"><span>Settings saved!</span></div>';
}
$api_key = get_option('smsleopard_api_key', '');
$api_secret = get_option('smsleopard_api_secret', '');
$sender_id = get_option('smsleopard_sender_id', '');
?>
<div class="wrap">
<h1>SMSLeopard Settings</h1>
<form method="post">
<table class="form-table">
<tr>
<th><label for="api_key">API Key</label></th>
<td>
<input type="text" name="api_key" id="api_key"
value="<?php echo esc_attr($api_key); ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th><label for="api_secret">API Secret</label></th>
<td>
<input type="password" name="api_secret" id="api_secret"
value="<?php echo esc_attr($api_secret); ?>"
class="regular-text" required>
</td>
</tr>
<tr>
<th><label for="sender_id">Sender ID</label></th>
<td>
<input type="text" name="sender_id" id="sender_id"
value="<?php echo esc_attr($sender_id); ?>"
class="regular-text" required>
<span class="description">Your approved SMS sender ID</span>
</td>
</tr>
</table>
<?php submit_button('Save Settings', 'primary', 'smsleopard_save_settings'); ?>
</form>
</div>
<?php
}
}
// Initialize the plugin
new SMSLeopard_WooCommerce();
Method 2: Functions.php Integration
For simpler setups, add this to your theme's functions.php:
<?php
// Add to functions.php
add_action('woocommerce_thankyou', 'send_order_confirmation_sms', 10, 1);
function send_order_confirmation_sms($order_id) {
$order = wc_get_order($order_id);
$phone = $order->get_billing_phone();
if (!$phone) {
return;
}
$api_key = 'YOUR_API_KEY';
$api_secret = 'YOUR_API_SECRET';
$sender_id = 'YOUR_SENDER_ID';
$message = sprintf(
'Thank you for your order #%s! Total: %s %s',
$order->get_order_number(),
$order->get_currency(),
$order->get_total()
);
$data = [
'source' => $sender_id,
'destination' => [format_phone_number($phone)],
'message' => $message
];
wp_remote_post('https://api.smsleopard.com/v1/sms/send', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key . ':' . $api_secret
],
'body' => json_encode($data)
]);
}
function format_phone_number($phone) {
$phone = preg_replace('/[^0-9]/', '', $phone);
if (substr($phone, 0, 1) === '0') {
return '254' . substr($phone, 1);
}
return $phone;
}Advanced Features
Shipping Tracking Updates
Integrate with shipping providers:
add_action('woocommerce_order_status_changed', 'send_tracking_update_sms', 10, 4);
function send_tracking_update_sms($order_id, $old_status, $new_status, $order) {
if ($new_status !== 'shipped') {
return;
}
$tracking_number = get_post_meta($order_id, '_tracking_number', true);
$carrier = get_post_meta($order_id, '_shipping_carrier', true);
$phone = $order->get_billing_phone();
if (!$phone || !$tracking_number) {
return;
}
$message = sprintf(
'Your order #%s has shipped via %s. Tracking: %s',
$order->get_order_number(),
$carrier,
$tracking_number
);
send_sms_notification($phone, $message);
}Customer Groups
Send targeted messages to specific customer groups:
function send_bulk_sms_to_customer_group($group_id, $message) {
$customers = get_users([
'meta_key' => 'customer_group',
'meta_value' => $group_id
]);
foreach ($customers as $customer) {
$phone = get_user_meta($customer->ID, 'billing_phone', true);
if ($phone) {
send_sms_notification($phone, $message);
}
}
}