不确定这是否对任何人有帮助,但我最近开设了一家 woocommerce 商店,店主在现场无法整天查看他们的电子邮件以获取订单。
因此,我在这里混合了 Hasan 和 Gary 的帖子,将它们合并为一个,以便在有新订单时使用 Twilio 通过 SMS 发出通知。
只需将以下内容添加到您的functions.php 并替换accountSID、authKey、SendNumber 和contactNumber 值。当然,也可以根据自己的喜好更改消息。
也用特殊字符(如 ÅÄÖ)对其进行了测试,它也有效。
//Send SMS Notification to admin
add_action('woocommerce_order_status_processing', 'send_woo_order_sms', 10, 3);
function send_woo_order_sms ($order_id){
//Get order data
$order = new WC_Order($order_id);
//Get first name from order
$billing_first_name = $order->get_billing_first_name();
//Get last name from order
$billing_last_name = $order->get_billing_last_name();
// Change to your Twilio account SID
$accountSID = 'xxxxxxxxxxxxxxxx';
// Change to your Twilio account Auth Key
$authKey = 'xxxxxxxxxxxxxxxx';
// Change to your Twilio account number
$sendNumber = '+xxxxxxxxxxxxxxxx';
//Change to your verified caller number (receiver of the sms)
$contactNumber = '+xxxxxxxxxxxxxxxx';
//Message to send
$smsMessage = "$billing_first_name $billing_last_name has just placed an order. See order #$order_id.";
// The Twilio API Url
$url = "https://api.twilio.com/2010-04-01/Accounts/".$accountSID."/Messages.json";
// The data being sent to the API
$data = array(
'From' => $sendNumber,
'To' => $contactNumber,
'Body' => $smsMessage
);
// Set the authorisation header
$headers = array( 'Authorization' => 'Basic ' . base64_encode($accountSID . ':' . $authKey));
// Send the POST request and store the response in a variable
$result = wp_remote_post($url, array( 'body' => $data, 'headers' => $headers));
return $order_id;
}
我想可以很容易地将其更改为发送给客户,而不是通过将联系号码更改为计费电话。
$contactNumber = $order->get_billing_phone();
然而,这需要 Twilio 的付费计划。