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 = "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?

Read more here: Fetching order data in new order hook

Leave a Reply

Your email address will not be published. Required fields are marked *