发送短信以获取特定的电子邮件通知和订单状态 [英] Sending an SMS for specific email notifications and order statuses

查看:132
本文介绍了发送短信以获取特定的电子邮件通知和订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我使用一个特殊的主题来处理摩托车和踏板车租赁服务的预订.我想获取与订单相关的数据.当我将电子邮件通知发送给 completed on hold pending 和*的客户时,我正在尝试发送SMS * processing**订单状态.

With WooCommerce I use a special theme that handle bookings for motorbikes and scooters rental service. I want to get the order related data. I am trying to send an SMS when an email notification is sent to customer for completed, on hold, pending and **processing** order status.

例如,我使用以下代码在SMS中输出所需的数据:

I have use the code below for instance that output the data I need in SMS:

$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个错误,或者什么也没有发生.

This is not working. Where should I hook this code? I tried different templates and all I got were some 500 errors or simply nothing happened.

请给我一些帮助.

谢谢

推荐答案

您可以使用包含在 woocommerce_email_order_details 钩子中的自定义函数,并使用随附的 $order $email 对象.

You can use a custom function hooked in woocommerce_email_order_details hook, using included $order and $email objects.

您将可以根据需要重新排列消息,这只是一个示例.

You will be able to rearrange the message as you like, as this is just an example.

我对这段代码进行了注释,以使您了解其工作原理:

I have commented this code to make you understand how it works:

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;

}

此代码适用于订单的第一项,前提是当时人们租了一辆自行车或一辆小型摩托车.
该代码主要经过测试,但是由于无法在您的SMS API上对其进行测试,因此我无法保证已触发的SMS.我希望这能奏效……让我知道.

This code will work for the first item of the order, assuming that people rent one bike or one scooter at the time.
The code is mainly tested, but I can't guaranty the triggered SMS as I can't test it on your SMS API. I hope this will work… Let me know.

代码进入您的活动子主题(或主题)的function.php文件中.或在任何插件php文件中.

这篇关于发送短信以获取特定的电子邮件通知和订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆