Marcus Christiansen
Marcus Christiansen

Reputation: 3197

woocommerce_new_order action not returning all order details

I'm using the woocommerce_new_order action to send order details to a Mailchimp list.

// Add user to mailchimp list
function add_user_to_mailchimp($order_id) {
    $order = get_post_meta($order_id);

    write_log($order);

}
add_action( 'woocommerce_new_order', 'add_user_to_mailchimp', 1, 1 );

An important part to keep in mind is that the user may not necessarily register an account.

The most important part is retrieving the the customers email and sending that through to Mailchimp. I want to achieve this by using

get_post_meta($order_id, 'billing_email', true);

but this returns an empty value even if I use '_billing_email'.

When I output the $order in the log I get the following output:

[17-Jan-2017 07:47:35 UTC] Array
(
     [_wc_customer_order_csv_export_is_exported] => Array
        (
            [0] => 0
        )

    [_wc_customer_order_csv_export_customer_is_exported] => Array
        (
            [0] => 0
        )

    [_order_key] => Array
        (
            [0] => wc_order_587dcc17ca5f0
        )

    [_order_currency] => Array
        (
            [0] => ZAR
        )

    [_prices_include_tax] => Array
        (
            [0] => yes
        )

    [_customer_ip_address] => Array
        (
            [0] => ::1
        )

    [_customer_user_agent] => Array
        (
            [0] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:50.0) Gecko/20100101 Firefox/50.0
        )

    [_customer_user] => Array
        (
            [0] => 0
        )

    [_created_via] => Array
        (
            [0] => checkout
        )

    [_cart_hash] => Array
        (
            [0] => 4457fd575a37c07e4863ce5610ddb4d2
        )

    [_order_version] => Array
        (
            [0] => 2.6.4
        )

)

Am I perhaps using the wrong action hook? All the values to exist in the database.

Upvotes: 2

Views: 3321

Answers (3)

Maksim Gerasimenko
Maksim Gerasimenko

Reputation: 53

and finaly you can use like it

`add_action('template_redirect', [$this, 'paymentRedirect']);`
    public function paymentRedirect()
    {
        if (is_wc_endpoint_url('order-received')) {
            global $wp;

            $int = filter_var($wp->request, FILTER_SANITIZE_NUMBER_INT);
            $orderId = str_replace('-', '', $int);

           SomeClass::doSomethink($orderId);
        }
    }

Upvotes: 0

Ashish Patel
Ashish Patel

Reputation: 3614

Try below teo hook in which pass order id as argument:

woocommerce_thankyou
woocommerce_payment_complete

Upvotes: 0

Marcus Christiansen
Marcus Christiansen

Reputation: 3197

Changed the action to

add_action( 'woocommerce_checkout_update_order_meta', 'add_user_to_mailchimp', 1, 1 );

This action fires after the post meta is created, which wasn't the case with 'woocommerce_new_order'.

Upvotes: 5

Related Questions