将SMS api与woocommerce集成,不发送消息

时间:2015-12-22 12:54:56

标签: wordpress woocommerce

我将SMS API与WooCommerce集成,以便在网站上进行任何购买时向客户手机发送自动订单更新。

下面是我的相同代码

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;
}

是对的吗? 我已将此代码添加到functions.php页面

我的短信网关提供商向我发送了以下示例代码,用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);

&GT;

5 个答案:

答案 0 :(得分:3)

为每个事件集成消息API将很困难,您需要在代码级别自定义。

您可以使用流行的SMSplugin进行wordpress woocommerce

https://wordpress.org/plugins/woocommerce-apg-sms-notifications/

只需要从woocommerce管理员登录配置此插件,它就会自动发送短信通知。我们正在使用它Spring Edge sms gateway ..工作正常。

答案 1 :(得分:0)

您的功能中的$ billingphone是ID。请尝试以下代码。它的工作原理

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
//Lets get data about the order made
$order = new WC_Order( $order_id );

//Now will fetch customer/buyer id here
$customer_id = $order->user_id;

//now finally we fetch phone number 
$billing_phone = get_user_meta( $customer_id, 'billing_phone', true );

// Now put your HTTP SMS API URL . I PUT WHICH WE ARE USING
$jsonurl = "http://tsms.thirdeyegoa.com/api/sendmsg.php?user=USERNAME&pass=PASSWORD&sender=MYSENDERID&phone=".$billing_phone."&priority=ndnd&stype=normal&text=MY MESSAGE TO CUSTOMER.";

// NOW WILL CALL FUNCTION CURL  
$json = curl($jsonurl);

return $order_id;
}

您也可以使用相应的挂钩完成订单已完成。

答案 2 :(得分:0)

我写了这段代码,它正在运作。

add_action('woocommerce_order_status_completed','payment_complete');
  function payment_complete($order_id)
{

$order=new Wc_order($order_id);
$order_meta = get_post_meta($order_id);
$shipping_first_name = $order_meta['_shipping_first_name'][0];
$phone=$order_meta['_billing_phone'][0];
$first_name=$order->billing_first_name;
$last_name=$order->billing_last_name;
$name=$first_name."".$last_name;
$mobile=$order->billing_mobile;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$time=$item['timings'];
$slot=$item['time_slot'];
$qty= $item['qty'];
$date=$item['tour_date'];
}
$text="your message here";
$data="user=username&pass=password&sender=sendername&phone=".$phone."&priority=ndnd&stype=normal&text=".$text;
$ch = curl_init('http://bhashsms.com/api/sendmsg.php?');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
 echo $result; 
curl_close($ch);
}

答案 3 :(得分:0)

我来自Textlocal。让我帮你解决问题。

首先是你得到的错误 - &#39;意外的令牌&#39;通常是由于无效的JSON响应。如果您不知道问题的根源,我建议您查看Dev部分的Javascript控制台(触发错误)。作为参考,您可以查看Mike的博客here

在您共享的代码中,Textlocal API未接收成功POST调用所必需的参数。您可以像这样重写您的功能:

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {

$order = new WC_Order( $order_id );
$customer_id = $order->user_id; 
$billing_phone = get_user_meta( $customer_id, 'billing_phone', true );

$data="username=USERNAME&hash=hash&sender=ORNGMT&numbers=".$billing_phone."&message=MY MESSAGE TO CUSTOMER.";
$jsonurl = curl_init('https://api.textlocal.in/send?');

$json = curl($jsonurl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($json);
echo $result; 
curl_close($json);

return $order_id;
}

如果您需要任何进一步的帮助,请联系我们的支持团队(support@textlocal.in)

答案 4 :(得分:0)

经过测试,可以正常工作 自定义api与Woocommerce wordpress集成

Ufone巴基斯坦短信与woocommerce wordpress集成

如果要与woocommerce wordpress的ufone巴基斯坦sms api bsms ufone巴基斯坦服务提供商集成,请在函数文件中使用以下代码

sms api将ufone bsms与wordpress woocommerce集成到此页面上感谢作者将自定义SMS API与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;
}