I’m integrating SMS API with WooCommerce to send automatic order updates to customers mobiles whenever the make any purchase on site.
below is my code for the same
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($billing_phone)
{
$username = "my username";
$hash = "d761fbd7bd31c5eeec2a5b2556d6b9d3b1a1ae51";
//Multiple mobiles numbers separated by comma
$mobileNumber = "$billing_phone";
$senderId = "ORNGMT";
$message = urlencode("Dear Customer");
$postData = array(
'hash' => $hash,
'mobiles' => $$billing_phone,
'message' => $message,
'sender' => $senderId,
);
$url='http://api.textlocal.in/send/?';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
curl_close($ch);
echo $output;
}
is it correct ?
I’ve added this code to functions.php page
my sms gateway provider has sent me below example code to send sms with PHP
<?php
// Authorisation details.
$username = "your login id";
$hash = "your hash key";
// Configuration variables. Consult http://api.textlocal.in/docs for more info.
$test = "0";
// Data for text message. This is the text message data.
$sender = "API Test"; // This is who the message appears to be from.
$numbers = "44777000000"; // A single number or a comma-seperated list of numbers
$message = "This is a test message from the PHP API script.";
// 612 chars or less
// A single number or a comma-seperated list of numbers
$message = urlencode($message);
$data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
$ch = curl_init('http://api.textlocal.in/send/?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); // This is the result from the API
curl_close($ch);
?>
Integrating message API for each event will be difficult and you need to customize the at the code level.
You can use a popular SMSplugin for wordpress woocommerce
https://wordpress.org/plugins/woocommerce-apg-sms-notifications/
Just you need to configure this plugin from woocommerce Admin login and it will send sms notifications automatically. We are using it with Spring Edge sms gateway.. Working Good.
Tested and working
Custom api Integration with Woocommerce wordpress
Ufone pakistan sms integration with woocommerce wordpress
if you are looking for integration with ufone pakistan sms api bsms ufone pakistan service provider with woocommerce wordpress then use the following code in your functions file
sms api integration ufone bsms with wordpress woocommerce thanks to the author on this page Integrating custom SMS API with woocommerce
$billingphone in your function is ID. Try this below code. It works
Same you can do for Order Completed too with respective hook.
i write this piece of code and it is working.
I am from Textlocal. Let me help you out with the code.
Firstly the error that you are getting – ‘Unexpected Token’ is generally due to an invalid JSON response. If you are unaware of the source of the issue, I would suggest you to look in the Javascript console in the Dev section (triggering the error). For reference, you can check Mike’s blog out here.
In the code you have shared, Textlocal API is not receiving the parameters that are mandatory for a successful POST call. You can rephrase your function like this:
If you need any further assistance, please reach out to our support team (support@textlocal.in)