简体   繁体   中英

Integrating custom SMS API with woocommerce

So I have been working on a project to integrate custom SMS API with woocommerce and wc vendor plugins. Unfortunately, I didn't find any particular solution for this. Everyone was talking about some plugins who actually support existing gateways. I was wondering what if someone wants to integrate own api with woocommerce!

Finally, I have come up with own code which is given below. The code goes to function.php in your child theme. FYKI, I had to use rawurlencode to encode the text message as some telcos require encoded message.

Thank you.

Special thanks to: Integrating SMS api with woocommerce , Not sending messages

//DYNAMIC ORDER MSG TO CUSTOMER 
add_action('woocommerce_order_status_processing', 'custom_msg_customer_process_order', 10, 3);

function custom_msg_customer_process_order ($order_id) {
//Lets get data about the order made
$order = new WC_Order($order_id);

//Now will fetch billing phone
$billing_phone = $order->get_billing_phone();
$billing_name = $order->get_billing_first_name();

$textmessage = rawurlencode("Dear $billing_name, Thank you for your order. Your order #$order_id is being processed. Please wait for confirmation call.");

// Now put HTTP SMS API URL
$url = "http://msms.THE_COMPANY.com/RequestSMS.php?user_name=YOUR_USER_NAME&pass_word=YOUR_PASSWORD&brand=YOUR_BRAND_NAME&type=1&destination=$billing_phone&sms=$textmessage";

// NOW WILL CALL FUNCTION CURL  
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);

return $order_id;
}

To integrate Datagen SMS API scripts into Wordpress Woocommerce without any plugins the just add Funcation.php Code into the Thousands of customers worldwide trust Datagen for sending millions of messages to their customers. The single SMS API integration service can transform your business and take it to great heights. With it, you can target customers globally as SMS-enabled any application, system or website facilitate international two way messaging by shortcodes, long numbers and more.

Dategen SMS API will Send Message whenever any purchase on site

Wordpress WooCommerce to send automatic order updates to customers mobiles whenever the make any purchase on site.

How to integrate sms api in wordpress woocommerce

Two things you keep before you are going to setup this dataen API

auth Key
Sender ID

Now , You need to added this code to functions.php page

add_action('woocommerce_thankyou', 'send_sms_api', 10, 1);

function send_sms_api($billing_phone)
{   

$results = $GLOBALS['wpdb']->get_results("SELECT * FROM wp_postmeta WHERE post_id = $billing_phone AND meta_key IN ('_billing_phone')", OBJECT);

    $mobilenois=$results[0]->meta_value;    
$auth = "Enter Your Auth Key Here "; // add auth Key Here  
 $mobileNumber = $mobilenois;
$senderId = "Enter Send ID Here"; // add Sender ID 
$message = urlencode("Thannk you for order with us. "); // Add Message here 
$postData = array(
    'auth' => $auth,
    'msisdn' => $mobileNumber,
    'message' => $message,
    'senderid' => $senderId,
);
$url='http://sms.datagenit.in/API/sms-api.php';
$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);
 $output;
}

https://datagenit.com/api-developer.php

Free Technical Support : Call On +91-9999-706-864 or mail us : hello@datagenit.com

I recently had this challenge myself, I had found an easy way to send notification SMS messages from WooCommerce with a plugin but I needed something to send messages in my own way.

I ended up using the Twilio service along with their API, I wrote up this tutorial as to how you can get it all set up so hope it helps!

function wpcodetips_send_sms( $smsMessage , $contactNumber ){
// Change to your account SID
$accountSID = 'XXXXXXXXXXXXX'; 
// Change to your account Auth Key
$authKey = 'XXXXXXXXXXXXX';
// Change to your account trial number
$sendNumber = '+XXXXXXXXXXX';
// The Twilio API Url 
$url = "https://api.twilio.com/2010-04-01/Accounts/".$accountSID."/Messages.json";
// The data being sent to the API
$data = array(
            'From' => $sendNumber,
            'To' => $contactNumber,
            'Body' => $smsMessage
        ); 
// Set the authorisation header
$headers = array( 'Authorization' => 'Basic ' . base64_encode($accountSID . ':' . $authKey));
// Send the POST request and store the response in a variable
$result = wp_remote_post($url, array( 'body' => $data, 'headers' => $headers));
// Return the response body to ensure it has worked
return json_decode($result['body'], true);
}

https://www.wpcodetips.com/wordpress-tutorials/how-to-send-an-sms-from-wordpress-programmatically/

Not sure if this helps anyone but I recently set up a woocommerce shop where the owners are not able to check their email for orders all day as they are out on the field.

So with the mix of Hasan's and Gary's post right here I consolidated them into one to make a notification over SMS using Twilio when a new order comes in.

Just add the below to your functions.php and replace the accountSID, authKey, SendNumber and contactNumber values. And of course change the message to your preference.

Tested it with special characters like ÅÄÖ as well and it worked.

//Send SMS Notification to admin
add_action('woocommerce_order_status_processing', 'send_woo_order_sms', 10, 3);

function send_woo_order_sms ($order_id){
    //Get order data
    $order = new WC_Order($order_id);
    //Get first name from order
    $billing_first_name = $order->get_billing_first_name();
    //Get last name from order
    $billing_last_name = $order->get_billing_last_name();
    // Change to your Twilio account SID
    $accountSID = 'xxxxxxxxxxxxxxxx';
    // Change to your Twilio account Auth Key
    $authKey = 'xxxxxxxxxxxxxxxx';
    // Change to your Twilio account number
    $sendNumber = '+xxxxxxxxxxxxxxxx';
    //Change to your verified caller number (receiver of the sms)
    $contactNumber = '+xxxxxxxxxxxxxxxx';
    //Message to send
    $smsMessage =  "$billing_first_name $billing_last_name has just placed an order. See order #$order_id.";
    // The Twilio API Url 
    $url = "https://api.twilio.com/2010-04-01/Accounts/".$accountSID."/Messages.json";
    // The data being sent to the API
    $data = array(
                'From' => $sendNumber,
                'To' => $contactNumber,
                'Body' => $smsMessage
            ); 
    // Set the authorisation header
    $headers = array( 'Authorization' => 'Basic ' . base64_encode($accountSID . ':' . $authKey));
    // Send the POST request and store the response in a variable
    $result = wp_remote_post($url, array( 'body' => $data, 'headers' => $headers));

    return $order_id;
}

I guess it could easily be changed to be sent out to the customer instead by changing the contactNumber to the billing phone.

$contactNumber = $order->get_billing_phone();

This would however require a paid plan at Twilio.

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

//add this line for calling your function on creation of order in woocommerce 
add_action('woocommerce_order_status_processing', 'custom_func', 10, 3);

function custom_func ($order_id) {

$order_details = new WC_Order($order_id);

//fetch all required fields
$billing_phone = $order_details->get_billing_phone();
$billing_name = $order_details->get_billing_first_name();
$billing_last_name = $order_details->get_billing_last_name();
$total_bill = $order_details->get_total();

$textmessage = rawurlencode("Dear $billing_name, Thank you for your order. Your order #$order_id is being processed. Please wait for confirmation call Total Bill = $total_bill");

// Now put HTTP ufone BSMS API URL
$url = "https://bsms.ufone.com/bsms_v8_api/sendapi-0.3.jsp?id=msisdn&message=$textmessage&shortcode=SHORTCODE&lang=English&mobilenum=$billing_phone&password=password&messagetype=Nontransactional";

// NOW WILL CALL FUNCTION CURL  
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);

return $order_id;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM