发送特定电子邮件通知和订单状态的短信
Sending an SMS for specific email notifications and order statuses
在 WooCommerce 中,我使用一个特殊的主题来处理摩托车和小型摩托车租赁服务的预订。我想获取订单相关数据。我正在尝试在向 completed
、on hold
、[= 的客户发送电子邮件通知时发送短信25=]pending
和 **processing**
订单状态。
我已经使用下面的代码在短信中输出我需要的数据:
$order = new WC_Order($order_id);
$status = $order->get_status(); // order status
if( 'completed' == $status || 'processing' == $status || 'pending' == $status || 'on-hold' == $status ){
$user_phone = get_post_meta($order_id, '_billing_phone', true);
foreach ($order->get_items() as $item_id => $item) {
$product_id = $order->get_item_meta($item_id, '_product_id', true); // product ID
$product_name = get_post($product_id)->post_title; // Product description
// Related Booking data to insert in SMS
$book_check_in = $order->get_item_meta( $item_id, '_st_check_in', true );
$book_check_out = $order->get_item_meta( $item_id, '_st_check_out', true );
$book_pick_up = $order->get_item_meta( $item_id, '_st_pick_up', true );
$book_drop_off = $order->get_item_meta( $item_id, '_st_drop_off', true );
}
// Send SMS in SMS API
file_get_contents("http://144.76.39.175/api.php?username=xxxxxxxxxxx&password=xxxxxxxxxxx&route=1&message%5B%5D=The+message&sender=NBWREN&mobile%5B%5D=xxxxxxxxxxx");
}
这是行不通的。我应该在哪里挂钩这段代码?我尝试了不同的模板,但我得到的只是大约 500 个错误,或者根本没有任何反应。
请帮帮我。
谢谢
您可以使用挂钩在 woocommerce_email_order_details
挂钩中的自定义函数,使用包含的 $order
和 $email
个对象。
您可以根据需要重新排列消息,因为这只是一个示例。
我对这段代码进行了评论,以便您了解它是如何工作的:
add_action('woocommerce_email_order_details', 'send_sms_on_email_notifications', 10, 4);
function send_sms_on_email_notifications($order, $sent_to_admin, $plain_text, $email){
$order_id = $order->id; // get the order ID for Order object
$email_id = $email->id; // get the email ID for Email object
$order_status = $order->get_status(); // Get order Status
// Array of Email IDs to avoid Admin email notifications (SMS sent twice on some notifications)
$emails_ids_exceptions = array('new_order', 'failed_order', 'customer_invoice', 'customer_note');
// Your targeted order status
$order_statuses = array('completed', 'processing', 'on-hold', 'pending');
$send_the_sms = false;
// Just for your targeted order statuses
if( in_array( $order_status, $order_statuses ) ):
// iterating in the order items
foreach($order->get_items() as $item_id => $item):
$prod_id = $order->get_item_meta( $item_id, '_product_id', true ); // product ID
$prod_name = get_post($prod_id)->post_title; // Product Name
$mobile = get_post_meta($order_id, '_billing_phone', true); // mobile phone
// Related Booking data to insert in SMS
$check_in = $order->get_item_meta( $item_id, '_st_check_in', true );
$check_out = $order->get_item_meta( $item_id, '_st_check_out', true );
$pick_up = $order->get_item_meta( $item_id, '_st_pick_up', true );
$drop_off = $order->get_item_meta( $item_id, '_st_drop_off', true );
// stoping the loop (just for one item)
break;
endforeach;
// Limiting to customer email notifications
if( !in_array( $email_id, $emails_ids_exceptions ) )
{
// inserting the order data (variables) in the message
$text = "Your order $order_id with $status status, for $prod_name. Your booking details: Check in time: $check_in, Check out Time: $check_out, Pick up $pick_up and drop of Time is $drop_off";
$send_the_sms = true;
}
// TRIGGERING THE SMS
if($send_the_sms)
{
// Replacing spaces by '+' in the message
$message = str_replace(' ', '+', $text);
// Inserting the message and the user number phone in the URL
$url = "http://144.76.39.175/api.php?username=xxxxxxxxxxx&password=xxxxxxxxxxx&route=1&message%5B%5D=$message&sender=NBWREN&mobile%5B%5D=$mobile";
// Triggering the SMS
file_get_contents($url);
}
endif;
}
假定人们当时租了一辆自行车或一辆踏板车,此代码将适用于订单的第一项。
代码主要是测试过的,但是我不能保证触发的短信,因为我不能在你的短信上测试它API。我希望这会奏效……让我知道。
代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。
在 WooCommerce 中,我使用一个特殊的主题来处理摩托车和小型摩托车租赁服务的预订。我想获取订单相关数据。我正在尝试在向 completed
、on hold
、[= 的客户发送电子邮件通知时发送短信25=]pending
和 **processing**
订单状态。
我已经使用下面的代码在短信中输出我需要的数据:
$order = new WC_Order($order_id);
$status = $order->get_status(); // order status
if( 'completed' == $status || 'processing' == $status || 'pending' == $status || 'on-hold' == $status ){
$user_phone = get_post_meta($order_id, '_billing_phone', true);
foreach ($order->get_items() as $item_id => $item) {
$product_id = $order->get_item_meta($item_id, '_product_id', true); // product ID
$product_name = get_post($product_id)->post_title; // Product description
// Related Booking data to insert in SMS
$book_check_in = $order->get_item_meta( $item_id, '_st_check_in', true );
$book_check_out = $order->get_item_meta( $item_id, '_st_check_out', true );
$book_pick_up = $order->get_item_meta( $item_id, '_st_pick_up', true );
$book_drop_off = $order->get_item_meta( $item_id, '_st_drop_off', true );
}
// Send SMS in SMS API
file_get_contents("http://144.76.39.175/api.php?username=xxxxxxxxxxx&password=xxxxxxxxxxx&route=1&message%5B%5D=The+message&sender=NBWREN&mobile%5B%5D=xxxxxxxxxxx");
}
这是行不通的。我应该在哪里挂钩这段代码?我尝试了不同的模板,但我得到的只是大约 500 个错误,或者根本没有任何反应。
请帮帮我。
谢谢
您可以使用挂钩在 woocommerce_email_order_details
挂钩中的自定义函数,使用包含的 $order
和 $email
个对象。
您可以根据需要重新排列消息,因为这只是一个示例。
我对这段代码进行了评论,以便您了解它是如何工作的:
add_action('woocommerce_email_order_details', 'send_sms_on_email_notifications', 10, 4);
function send_sms_on_email_notifications($order, $sent_to_admin, $plain_text, $email){
$order_id = $order->id; // get the order ID for Order object
$email_id = $email->id; // get the email ID for Email object
$order_status = $order->get_status(); // Get order Status
// Array of Email IDs to avoid Admin email notifications (SMS sent twice on some notifications)
$emails_ids_exceptions = array('new_order', 'failed_order', 'customer_invoice', 'customer_note');
// Your targeted order status
$order_statuses = array('completed', 'processing', 'on-hold', 'pending');
$send_the_sms = false;
// Just for your targeted order statuses
if( in_array( $order_status, $order_statuses ) ):
// iterating in the order items
foreach($order->get_items() as $item_id => $item):
$prod_id = $order->get_item_meta( $item_id, '_product_id', true ); // product ID
$prod_name = get_post($prod_id)->post_title; // Product Name
$mobile = get_post_meta($order_id, '_billing_phone', true); // mobile phone
// Related Booking data to insert in SMS
$check_in = $order->get_item_meta( $item_id, '_st_check_in', true );
$check_out = $order->get_item_meta( $item_id, '_st_check_out', true );
$pick_up = $order->get_item_meta( $item_id, '_st_pick_up', true );
$drop_off = $order->get_item_meta( $item_id, '_st_drop_off', true );
// stoping the loop (just for one item)
break;
endforeach;
// Limiting to customer email notifications
if( !in_array( $email_id, $emails_ids_exceptions ) )
{
// inserting the order data (variables) in the message
$text = "Your order $order_id with $status status, for $prod_name. Your booking details: Check in time: $check_in, Check out Time: $check_out, Pick up $pick_up and drop of Time is $drop_off";
$send_the_sms = true;
}
// TRIGGERING THE SMS
if($send_the_sms)
{
// Replacing spaces by '+' in the message
$message = str_replace(' ', '+', $text);
// Inserting the message and the user number phone in the URL
$url = "http://144.76.39.175/api.php?username=xxxxxxxxxxx&password=xxxxxxxxxxx&route=1&message%5B%5D=$message&sender=NBWREN&mobile%5B%5D=$mobile";
// Triggering the SMS
file_get_contents($url);
}
endif;
}
假定人们当时租了一辆自行车或一辆踏板车,此代码将适用于订单的第一项。
代码主要是测试过的,但是我不能保证触发的短信,因为我不能在你的短信上测试它API。我希望这会奏效……让我知道。
代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。