简体   繁体   中英

Manually Sending A New Order Email In Woocommerce

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?

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+

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM