[英]TextLocal.in SMS Api for new order Woocommerce
我正在使用以下 API 代码在下新订单时发送短信,短信 API 代码正在发送短信...放置在功能中子主题的末尾。php 文件...所有更新(WordPress 5.9.3 和 woo-commerce 6.4 PHP 8.0)
2个问题:
尝试了以下内容:
文档: 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
)那么它可能不会触发......因为它只会在从pending
到processing
的转换时触发。 更好的钩子实际上可能是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.