首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >面向新订单的TextLocal.in短消息应用程序

面向新订单的TextLocal.in短消息应用程序
EN

Stack Overflow用户
提问于 2022-04-15 07:20:42
回答 1查看 171关注 0票数 0

我使用下面的API代码发送短信当一个新的订单时,SMS API代码正在工作发送SMS .放置在functions.php文件的子主题末尾.所有更新(WordPress 5.9.3和6.4与PHP8.0)

2期:

variables.

  • When,,
  1. ,$order_id,$order_date不填充给定的变量,而短消息是通过2
  2. 接收到的,订单是由客户下的,这段代码被触发,即使没有付款,也会收到短消息,后端的订单状态显示正在等待付款。

试过以下几点:

  1. 在第一期中,我将消息变量更改为“$order_id”或“.$order_id”。但是它没有工作,wp崩溃了,所以必须将普通的$order_id...
  2. For保持在第二个问题上,我将钩子改为'woocommerce_order_status_processing‘,但是这段代码不适用于一个新的订单。

文档:https://www.textlocal.in/free-developer-sms-api/

有任何建议来修改代码以便解决这两个问题吗?

谢谢

代码语言:javascript
复制
// Sending SMS to customers on new orders
add_action('woocommerce_new_order', 'custom_msg_customer_process_order', 10, 3);
function custom_msg_customer_process_order ($order_id) {

$order = new WC_Order( $order_id );
$order_date = $order->get_date_created();
$billing_phone = $order->get_billing_phone();

$apiKey = urlencode('apikey');

// Message details
$numbers = array($billing_phone,91xxxxxxxxxx);
$sender = urlencode('TXTCL');
$message = rawurlencode('Thank you for buying from us, a Wellness product. Your order number $order_id Dated $order_date is confirmed.');

$numbers = implode(',', $numbers);

// Prepare data for POST request
$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);

// Send the POST request with cURL
$ch = curl_init('https://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// Process your response here
echo $response;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-15 13:37:51

虽然我对文本API不太了解(我当然希望您的客户选择使用文本更新!),但我认为new WC_Order从未从数据库中读取您的数据。如何获得一个新的order对象是wc_get_order()。但是,woocommerce_new_order钩子沿$order object as the 2nd parameter传递。

因此,如果我们确保回调需要第二个参数,就不需要重新实例化order对象。

至于第2部分,当订单保存到DB时,woocommerce_new_order就会触发,而订单状态是什么并不重要。相反,我认为我们可以使用woocommerce_order_status_pending_to_processing,这是新订单电子邮件使用的方式。

代码语言:javascript
复制
/**
 * Sending SMS to customers on new orders.
 * 
 * @param int $order_id The order ID. *
 * @param WC_Order $order Order object.
 */
function custom_msg_customer_process_order( $order_id, $order ) {

    $order_date = $order->get_date_created();
    $billing_phone = $order->get_billing_phone();

    $apiKey = urlencode('apikey');

    // Message details
    $numbers = array($billing_phone,91xxxxxxxxxx);
    $sender = urlencode('TXTCL');

    // Use sprintf() to replace placeholders with values.
    $message = rawurlencode( sprintf( 'Thank you for buying from us, a Wellness product. Your order number %s Dated %s is confirmed.', $order_id, $order_date ) );

    $numbers = implode(',', $numbers);

    // Prepare data for POST request
    $data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);

    // Send the POST request with cURL
    $ch = curl_init('https://api.textlocal.in/send/');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    // Process your response here
    echo $response;
}
add_action('woocommerce_order_status_pending_to_processing', 'custom_msg_customer_process_order', 10, 2 );

替代

以上情况取决于您的网关将订单设置为processing。如果它没有(或者不是最初的pending),那么它可能不会开火.因为它只在从pendingprocessing的转换中触发。一个更好的钩子可能实际上是woocommerce_payment_complete。但请注意,在源中,它只向其回调传递一个参数.$order_id (类似于woocommerce_thankyou钩子)。因此,需要对代码片段进行调整,以便只需要一个参数:

代码语言:javascript
复制
/**
 * Sending SMS to customers on new orders.
 * 
 * @param int $order_id The order ID. *
 */
function custom_msg_customer_process_order( $order_id ) {

    $order = wc_get_order( $order_id );

    // Quit early if not a valid order.
    if ( ! $order ) {
         return;
    }

    $order_date = wc_format_datetime( $order->get_date_created() );
    $billing_phone = $order->get_billing_phone();

    $apiKey = urlencode('apikey');

    // Message details
    $numbers = array($billing_phone,91xxxxxxxxxx);
    $sender = urlencode('TXTCL');
    
    // Use sprintf() to replace placeholders with values.
    $message = rawurlencode( sprintf( 'Thank you for buying from us, a Wellness product. Your order number %s Dated %s is confirmed.', $order_id, $order_date ) ) );

    $numbers = implode(',', $numbers);

    // Prepare data for POST request
    $data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);

    // Send the POST request with cURL
    $ch = curl_init('https://api.textlocal.in/send/');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    // Process your response here
    echo $response;
}
add_action('woocommerce_payment_complete', 'custom_msg_customer_process_order' );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71881179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档