3

I'm trying to send myself an email after each placed order. The issue I have is that $order->get_total() as well as get_total_tax return 0 instead of the actual order total value.

add_action( 'woocommerce_new_order', 'custom_after_order_created_hook', 12 , 1);
function custom_after_order_created_hook($order_id) {
    $order = new WC_Order($order_id);

    $with_tax = $order->get_total();
    $tax = $order->get_total_tax();
    $without_tax = $with_tax - $tax;

    $to = "test@example.com";
    $subject = "New order";
    $content = "
    New order {$order->id}
    With tax: {$with_tax}
    Without tax: {$without_tax}
    Tax: {$tax}
    ";

    $status = wp_mail($to, $subject, $content);
}

Every value besides $order_id and $order->id gets evaluated to 0. $order_id has proper value. This issue happens only when using woocommerce_new_order hook (I also tried using it on a custom page - works properly), which makes me wonder.

Im not sure what is the issue here, is some part of my code async?
Or maybe this hook is called before order gets updated with price paid/tax info?
What should I do in order to get price info here?

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
mmln
  • 2,104
  • 3
  • 24
  • 33

1 Answers1

7

This woocommerce_new_order action hook is used to alter the create_order() function. So you better use woocommerce_thankyou action hook that will trigger your custom email notification when order has been created:

// Tested on WooCommerce versions 2.6+ and 3.0+
add_action( 'woocommerce_thankyou', 'new_order_custom_email_notification', 1, 1 );
function new_order_custom_email_notification( $order_id ) {
    if ( ! $order_id ) return;

    // Getting an instance of WC_Order object
    $order = wc_get_order( $order_id );

    $with_tax = $order->get_total();
    $tax = $order->get_total_tax();
    $without_tax = $with_tax - $tax;
            
    $to = "test@example.com";
    $subject = "New order";
    $content = "
    New order {$order_id}
    With tax: {$with_tax}
    Without tax: {$without_tax}
    Tax: {$tax}
    ";
    
    wp_mail($to, $subject, $content);
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Code is tested and works.

Using woocommerce_checkout_order_processed action hook instead of woocommerce_thankyou action hook is also a good alternative, may be even better. You have just to replace:

add_action( 'woocommerce_thankyou', 'new_order_custom_email_notification', 1, 1 );

By:

add_action( 'woocommerce_checkout_order_processed', 'new_order_custom_email_notification', 1, 1 );

Similar working Answer: Woocommerce - How to send custom emails based on payment type


The woocommerce_checkout_order_processed hook (located in WC_Checkout process_checkout() method that could be convenient too for this purpose.

The source code of WC_Checkout process_checkout() method is interesting to get a view on the purchase flow.

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thank you, thats what i suspected. Do you by any chance know wether the whole purchase flow is documented somewhere? Official docs arent very helpful so far. – mmln Apr 14 '17 at 19:47
  • 1
    @mymlyn I have maid an update on the answer regarding the purchase flow in WooCommerce (at the end). I hope that this will help you; So you can either use woocommerce_checkout_order_processed – LoicTheAztec Apr 14 '17 at 21:26