Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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?

Thanks.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda