简体   繁体   English

TextLocal.in SMS Api 新订单 Woocommerce

[英]TextLocal.in SMS Api for new order Woocommerce

I am using the following API code to send an SMS when a new order is placed, the SMS API code is working to send SMS... Placed at the end of child theme in functions.php file... all is updated(WordPress 5.9.3 and woo-commerce 6.4 with PHP 8.0)我正在使用以下 API 代码在下新订单时发送短信,短信 API 代码正在发送短信...放置在功能中子主题的末尾。php 文件...所有更新(WordPress 5.9.3 和 woo-commerce 6.4 PHP 8.0)

2 Issues: 2个问题:

  1. The $order_id and $order_date do not populate in the given variables and the SMS is received as is with the 2 variables. $order_id 和 $order_date 不填充给定的变量,并且 SMS 与 2 个变量一样接收。
  2. When the order is placed by a customer this code is triggered and SMS is received even when the payment is not made and the order status in the backend is showing pending payment.当客户下订单时,即使未付款且后台订单状态显示待付款,也会触发此代码并收到短信。

Tried the following:尝试了以下内容:

  1. For the 1st issue I changed the message variable to '$order_id' or '.$order_id.'对于第一期,我将消息变量更改为“$order_id”或“.$order_id”。 but it did not work and wp crashed so had to keep plain $order_id...但它没有用,wp 崩溃了,所以不得不保持简单的 $order_id ...
  2. For the 2nd issue, I changed the hook to 'woocommerce_order_status_processing' but this code does not work for a new order.对于第二期,我将挂钩更改为“woocommerce_order_status_processing”,但此代码不适用于新订单。

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

Any suggestions to tweak the code so both the problems are solved?有什么建议可以调整代码以解决这两个问题吗?

Thanks谢谢

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

While I don't know much about the texting API (and I sure hope your customers are opting in to text updates,), what I think is happening is that new WC_Order never reads your data from the database.虽然我不太了解短信 API(我当然希望您的客户选择接收短信更新,),但我认为正在发生的事情是new WC_Order永远不会从数据库中读取您的数据。 How you should get a new order object is wc_get_order() .你应该如何获得新订单 object 是wc_get_order() However, the woocommerce_new_order hook passes along the $order object as the 2nd parameter但是, woocommerce_new_order挂钩将$order object 作为第二个参数传递

So if we make sure our callback is expecting a second parameter there's no need to re-instantiate the order object.因此,如果我们确保回调需要第二个参数,则无需重新实例化订单 object。

As for part 2, woocommerce_new_order will fire when the order is saved to the DB and it doesn't matter what the order status is.至于第 2 部分, woocommerce_new_order将在订单保存到数据库时触发,订单状态是什么并不重要。 Instead, I think we can use woocommerce_order_status_pending_to_processing which is what the new order email uses.相反,我认为我们可以使用新订单woocommerce_order_status_pending_to_processing使用的 woocommerce_order_status_pending_to_processing。

/**
 * 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 );

Alternative选择

The above depends on your gateway setting the order to processing .以上取决于您的网关设置processing顺序。 If it does not (or if it wasn't initially pending ) then it may not fire... since it only fires on the transition from pending to processing .如果它没有(或者如果它最初不是pending )那么它可能不会触发......因为它只会在从pendingprocessing的转换时触发。 A better hook might actually be woocommerce_payment_complete .更好的钩子实际上可能是woocommerce_payment_complete But note that in source, it only passes 1 parameter to its callbacks... the $order_id (similar to the woocommerce_thankyou hook does).但请注意,在源代码中,它只将 1 个参数传递给它的回调... $order_id (类似于woocommerce_thankyou挂钩)。 Therefore the code snippet would need to be adjusted to expect only a single parameter:因此,代码片段需要调整为只需要一个参数:

/**
 * 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' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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