簡體   English   中英

TextLocal.in SMS Api 新訂單 Woocommerce

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

我正在使用以下 API 代碼在下新訂單時發送短信,短信 API 代碼正在發送短信...放置在功能中子主題的末尾。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. 對於第二期,我將掛鈎更改為“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永遠不會從數據庫中讀取您的數據。 你應該如何獲得新訂單 object 是wc_get_order() 但是, woocommerce_new_order掛鈎將$order object 作為第二個參數傳遞

因此,如果我們確保回調需要第二個參數,則無需重新實例化訂單 object。

至於第 2 部分, woocommerce_new_order將在訂單保存到數據庫時觸發,訂單狀態是什么並不重要。 相反,我認為我們可以使用新訂單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 );

選擇

以上取決於您的網關設置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' );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM