代码之家  ›  专栏  ›  技术社区  ›  AgoraLive

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

  •  0
  • AgoraLive  · 技术社区  · 8 年前

    阅读以下内容: How to get WooCommerce order details 张贴,没有帮助我,我可以得到我需要的信息和订单页面失败时,我尝试。。。

    我今天的代码如下:

    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;
        }
    }
    

    我需要的是在邮件中包含名字。

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

    但似乎什么都不适合我。

    1 回复  |  直到 8 年前
        1
  •  2
  •   LoicTheAztec    8 年前

    相反,您可以尝试使用挂接在 woocommerce_thankyou

    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' );
    }
    

    代码进入函数。活动子主题(或主题)或任何插件文件中的php文件。


    相关短信回复:

    Sending an SMS for specific email notifications and order statuses