1

I am sending extra Mail after new woocommerce order.

I am using woo commerce_new_order hook.

Problem is that when the email arrives it hasn't had product info. I think that woocommerce_new_order hook fires before everything are stored in the database. Because if I run this with the existing order every info is included.

The question is how could I add a delay before data is fetched and email is sent?

add_action( 'woocommerce_new_order', 'extra_mail_after_new_order', 20, 1 );

function extra_mail_after_new_order( $order_id ) {
$order = wc_get_order( $order_id );

$items = $order->get_items();
foreach ( $items as $item ) {
    $product_name = $item->get_name();
    $product_id = $item->get_product_id();
    $product = wc_get_product($product_id);
    $product_variation_id = $item->get_variation_id();
    $product_data = $product->get_meta('extra_email')        
}


add_filter('wp_mail_content_type', function( $content_type ) {
    return 'text/html';
});


$to = 'mail.mail@gmail.com';
$subject = $product_name . ' uusi tilaus!';
$message = 'Order id: '. $order_id . '<br />product name: '. $product_name . '<br />product id: '. $product_id. '<br />product meta: '. $product_data. '<br />status: '. $status ;

wp_mail( $to, $subject, $message );   }
Ville
  • 105
  • 2
  • 2
  • 8
  • Why would you add a delay, just use another hook that fires after everything is stored in the database – 7uc1f3r Jun 03 '22 at 11:57
  • Thanks, would you idea what hook would fire later? I dint find suitable? – Ville Jun 03 '22 at 12:02
  • 1
    There are several. for questions like this, look for your current hook in the WooCommerce source and see which ones run next. For example `do_action( 'woocommerce_checkout_order_created', $order )` - Action hook fired after an order is created. – 7uc1f3r Jun 03 '22 at 12:12
  • 1
    ps. in your current foreach loop the values ​​are overwritten every time, so only for the last product ($item) in the loop the values ​​are stored – 7uc1f3r Jun 03 '22 at 12:19

1 Answers1

0

Problem was solved with this hook

woocommerce_booking_in-cart_to_pending-confirmation_notification
Ville
  • 105
  • 2
  • 2
  • 8