1

I would like to programmically fire a new order email, so it looks the same as the standard Woo Commerce email template.

I am using the WC_Email_New_Order class, so I can made adjustment to the order object before the email is constructed from it.

Here in some test code, I am firing it the wp_head hook for now, just for testing purposes.

add_action('wp_head', function() {
    include('wp-content/plugins/woocommerce/includes/emails/class-wc-email.php');
    include('wp-content/plugins/woocommerce/includes/emails/class-wc-email-new-order.php');

    $adminEmail = new WC_Email_New_Order();

    $id = 1564; // order
    $order = new WC_Order($id);

    $adminEmail->trigger( null, $order );

}); 

This works partially... I get the email with the correct subject line, however the body of the email just says...

You’ve received the following order from Dave Peterson:

How do I get the rest of the email template to fire?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Dale Woods
  • 784
  • 1
  • 13
  • 31

2 Answers2

7

You could simply try use the following in any hooked function with a dynamic variable $order_id:

// Get the WC_Email_New_Order object
$email_new_order = WC()->mailer()->get_emails()['WC_Email_New_Order'];

// Sending the new Order email notification for an $order_id (order ID)
$email_new_order->trigger( $order_id );

This normally will fire the New Order Woocommerce notification for the current order ID.

Since WooCommerce 5+: Allow re-sending New Order Notification in WooCommerce 5+

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
-1

You just need an order object to trigger new order emails. WC()->mailer()->emails['WC_Email_New_Order']->trigger( $order->get_id(), $order );