Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching order data in new order hook

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 = "[email protected]";
    $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.

like image 335
mmln Avatar asked Nov 28 '25 06:11

mmln


1 Answers

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 = "[email protected]";
    $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.

like image 81
LoicTheAztec Avatar answered Nov 30 '25 18:11

LoicTheAztec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!