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

Get customer data just before order-received in WooCommerce

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

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

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

读这个: post,对我没有帮助,我可以获得我需要的信息,但当我尝试时订单页面失败...

我今天的代码是这样的:

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'];

但似乎没有什么适合我。

相反,您可以尝试使用挂钩在 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+ 上测试过…


相关短信回复: