【问题标题】:Get customer data just before order-received in WooCommerce在 WooCommerce 中收到订单之前获取客户数据
【发布时间】:2018-01-20 22:07:18
【问题描述】:

在 WooCommerce 中,当我的网店收到新订单时,我正在运行一个脚本。此脚本向我发送一条短信,但我也想将其发送给客户。

脚本正在使用自定义函数脚本运行,就在包含订单信息的已收到订单页面之前。

如何从订单中获取有关用户使用的姓名和电话号码的自动信息?

阅读这个: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'];

但似乎没有什么对我有用。

【问题讨论】:

    标签: php wordpress woocommerce sms orders


    【解决方案1】:

    相反,您可以尝试使用挂钩在 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' );
    }
    

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

    在 WooCommerce 3+ 上测试...


    相关短信回复:

    Sending an SMS for specific email notifications and order statuses

    【讨论】:

    • 完美,第一部分没用!!!创建自定义函数是关键,谢谢....
    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多