TextLocal.in 短信 Api 新订单 Woocommerce

TextLocal.in SMS Api for new order Woocommerce

我正在使用以下 API 代码在下新订单时发送短信,短信 API 代码正在发送短信...放置在子主题的末尾在 functions.php 文件中...全部更新(WordPress 5.9.3 和 woo-commerce 6.4 与 PHP 8.0)

2 个问题:

  1. $order_id 和 $order_date 不填充给定的变量,SMS 会按原样接收 2 个变量。
  2. 当客户下订单时,即使未付款且后台订单状态显示待付款,也会触发此代码并收到短信。

尝试了以下方法:

  1. 对于第一期,我将消息变量更改为“$order_id”或“.$order_id”。但它没有用,wp 崩溃了,所以不得不保持原样 $order_id...
  2. 对于第 2 期,我将挂钩更改为 'woocommerce_order_status_processing' 但是此代码不适用于新订单。

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

有什么建议可以调整代码以解决这两个问题吗?

谢谢

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

虽然我对短信 API 了解不多(我当然希望您的客户选择接收短信更新!),但我认为正在发生的事情是 new WC_Order 从不阅读你的数据来自数据库。您应该如何获得新的订单对象是 wc_get_order()。然而,woocommerce_new_order 钩子通过 $order object as the 2nd parameter

因此,如果我们确保我们的回调需要第二个参数,则不需要 re-instantiate 订单对象。

至于第 2 部分,woocommerce_new_order 将在订单保存到数据库时触发,无论订单状态如何。相反,我认为我们可以使用 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 );

备选

以上取决于您的网关将顺序设置为 processing。如果它没有(或者如果它最初不是 pending)那么它可能不会触发......因为它只在从 pendingprocessing 的过渡时触发。更好的钩子实际上可能是 woocommerce_payment_complete。但请注意,在源代码中,它只将 1 个参数传递给它的回调......$order_id(类似于 woocommerce_thankyou 挂钩)。因此,需要调整代码片段以期望只有一个参数:

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