0

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
AgoraLive
  • 81
  • 3
  • 10

1 Answers1

2

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399