簡體   English   中英

Wordpress - Woocommerce,下單后通過Smsbroadcast發送短信API

[英]Wordpress - Woocommerce, Sending SMS through Smsbroadcast API after order has been placed

我需要在成功下單后給客戶發送短信。 我已經在 Smsbroadcast 注冊,他們提供 API 和 curl。 誰能幫我將它與 wordpress 集成?

<?php
function sendSMS($content) {
    $ch = curl_init('https://api.smsbroadcast.com.au/api-adv.php');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
    curl_close ($ch);
    return $output;    
}

$username = 'USERNAME';
$password = 'PASSWORD';
$destination = '0400000000'; //Multiple numbers can be entered, separated by a comma
$source    = 'MyCompany';
$text = 'This is our test message.';
$ref = 'abc123';
    
$content =  'username='.rawurlencode($username).
            '&password='.rawurlencode($password).
            '&to='.rawurlencode($destination).
            '&from='.rawurlencode($source).
            '&message='.rawurlencode($text).
            '&ref='.rawurlencode($ref);

$smsbroadcast_response = sendSMS($content);
$response_lines = explode("\n", $smsbroadcast_response);

 foreach( $response_lines as $data_line){
    $message_data = "";
    $message_data = explode(':',$data_line);
    if($message_data[0] == "OK"){
        echo "The message to ".$message_data[1]." was successful, with reference ".$message_data[2]."\n";
    }elseif( $message_data[0] == "BAD" ){
        echo "The message to ".$message_data[1]." was NOT successful. Reason: ".$message_data[2]."\n";
    }elseif( $message_data[0] == "ERROR" ){
        echo "There was an error with this request. Reason: ".$message_data[1]."\n";
    }
}

?>

您正在尋找的鈎子是woocommerce_thankyou

add_action( 'woocommerce_thankyou', 'post_transaction_after_order_completion',  10, 1 );

function post_transaction_after_order_completion( $order_id ) {
    $order = wc_get_order( $order_id ); // phpcs:ignore.

    $status = $order->get_status();

    // If this is executed once, no need to run it again.
    if ( ! ( '' === $order->get_meta( '_thank_you' ) ) ) {
        return;
    } else {
        $order->update_meta_data( '_thank_you', 'true' );
        $order->save();
    }

    // Use this to allow code to run when order status is completed or processing.
    if ( ! ( 'completed' === $status || 'processing' === $status ) ) {
        return;
    }

    $user = $order->get_user(); // Fetch all the other user data from here.

    $content = ""; // Create content that you want using $order & $user.

    sendSMS($content); // Use your send function to send API request.
}

筆記


  1. 了解更多關於從這里獲取訂單數據的信息。 https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
  2. 您可以使用 GuzzlePHP 進行 API 調用。 https://docs.guzzlephp.org/en/stable/index.html (不需要,Curl 工作正常)。
  3. 您的 API 調用將是阻塞代碼。 確保在請求中添加超時,以便結帳感謝頁面不會因為緩慢的 API 響應而永遠加載。

暫無
暫無

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

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