donglangtun1850 2017-08-12 14:21
浏览 67
已采纳

在WooCommerce订单收到之前获取客户数据

In WooCommerce, I have a script that I am running when my webshop get a new order. This script sends an SMS to me but I would like to send it to the customer as well.

The script is running using a custom function script, just before the Order-received page with information about the order.

How do I get automatic information from the order about the name and phone number the user used?

Reading this: How to get WooCommerce order details post, does not help me, I can get the information I need and the order page fails when I try...

my code today is like this:

add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' ); 
function wc_custom_redirect_after_purchase() {
    global $wp;

    if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {

    // Query args
    $query21 = http_build_query(array(
        'token' => 'My-Token',
        'sender' => 'medexit',
        'message' => 'NEW ORDER',
        'recipients.0.msisdn' => 4511111111,
    ));
    // Send it
    $result21 = file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query21);

    //      exit;
    }
}

What do I need is to get the firstname included in the message.

I would like something like:

$firstname = $order_billing_first_name = $order_data['billing']['first_name'];
$phone = $order_billing_phone = $order_data['billing']['phone'];

But nothing seems to works for me.

  • 写回答

1条回答 默认 最新

  • doomli3721 2017-08-12 15:16
    关注

    Instead you could try to use a custom function hooked in woocommerce_thankyou action hook:

    add_action( 'woocommerce_thankyou', 'wc_custom_sending_sms_after_purchase', 20, 1 );
    function wc_custom_sending_sms_after_purchase( $order_id ) {
        if ( ! $order_id ) return;
    
        // Avoid SMS to be sent twice
        $sms_new_order_sent = get_post_meta( $order_id, '_sms_new_order_sent', true );
        if( 'yes' == $sms_new_order_sent ) return;
    
        // Get the user complete name and billing phone
        $user_complete_name  = get_post_meta( $order_id, '_billing_first_name', true ) . ' ';
        $user_complete_name .= get_post_meta( $order_id, '_billing_last_name', true );
        $user_phone = get_post_meta( $order_id, '_billing_phone', true );
    
        // 1st Query args (to the admin)
        $query1 = http_build_query( array(
            'token' => 'My-Token',
            'sender' => 'medexit',
            'message' => 'NEW ORDER',
            'recipients.0.msisdn' => 4511111111
        ) );
    
        // 2nd Query args (to the customer)
        $query2 = http_build_query( array(
            'token' => 'My-Token',
            'sender' => 'medexit',
            'message' => "Hello $user_complete_name. This is your new order confirmation",
            'recipients.0.msisdn' => intval($user_phone)
        ) );
    
        // Send both SMS
        file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query1);
        file_get_contents('https://gatewayapi.com/rest/mtsms?' . $query2);
    
        // Update (avoiding SMS to be sent twice)
        update_post_meta( $order_id, '_sms_new_order_sent', 'yes' );
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested on WooCommerce 3+…


    Related SMS answer:

    Sending an SMS for specific email notifications and order statuses

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c