donglangtun1850 2017-08-12 06:21
浏览 68
已采纳

在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 07: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 怎么实现数组的循环累加,simulink
  • ¥15 51单片机最小开发板系统,想让寻迹小车在全检测到黑线(寻迹模块代码在第一块板子上)时蜂鸣器响(在第二块板子上)
  • ¥15 pbootcms多选调用成列表
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部