Allow Re-Sending New Order Notification in Woocommerce 5+

Allow re-sending New Order Notification in WooCommerce 5+

Since WooCommerce 5.0 a new filter hook has been added, that disables resending "New Order" email notification, restricting this specific notification to be sent only one time.

This is what has been added to WC_Email_New_Order trigger() method (default is set to false):

/**
* Controls if new order emails can be resend multiple times.
*
* @since 5.0.0
* @param bool $allows Defaults to true.
*/
if ( 'true' === $email_already_sent && ! apply_filters( 'woocommerce_new_order_email_allows_resend', false ) ) {
return;
}

So you need now to add this little additional piece of code, to unlock this notification:

add_filter('woocommerce_new_order_email_allows_resend', '__return_true' );

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Now your code will work again.

I have opened an issue on WooCommerce Github, as main hook argument should be set to true by default (as mentioned on the comment bloc), and should allow by default to resend New Order Notification.

Send WooCommerce New Order email notification only for paid orders with processing status

This is what I have managed to make work properly, removing all New Order triggers possibilities entirely (as provided here by woocommerce):

/**
* Unhook and remove WooCommerce all default "New Order" emails.
*/

add_action( 'woocommerce_email', 'unhook_those_pesky_emails' );

function unhook_those_pesky_emails( $email_class ) {
// New order emails
remove_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
remove_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
}

And using the following trigger (provided by @LoicTheAztec in this thread)

/** 
* trigger "New Order" email on "processing" status
*/

add_action( 'woocommerce_order_status_processing', 'process_new_order_notification', 20, 2 );
function process_new_order_notification( $order_id, $order ) {
// Send "New Email" notification (to admin)
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}

It's probably not clean and very likely not the most optimized way, but it is the only way I have found to successfully make "New Order" emails only be sent when orders have been paid (set to processing status) and hope it manages to help someone else.

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

Sending WooCommerce new order email notification to related managers

First you can't get the current user ID on email notifications, as it's a background process. What you can get is the customer ID that belongs to the order using the WC_Order method get_customer_id().

Now you get duplicated emails because there are some mistakes in your code, that can be simplified.

I suppose that "manager" is a custom user role as WooCommerce uses "shop_manager"

So try the following instead:

// Send mail to Manager on new order
add_filter('woocommerce_email_recipient_new_order', 'my_new_order_email_recipient', 10, 2);
function my_new_order_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) )
return $recipient;

$customer_id = $order->get_customer_id();
$customer_group = get_user_meta( $customer_id, 'group_meta_key', true );
$manager_emails = [];

// Get an array of WP_User from "manager" user role
$users = get_users(['role' => 'manager']);

if ( count($users) > 0 ) {
foreach ( $users as $user ) {
if ( $customer_group === get_user_meta( $user->ID, 'group_meta_key', true ) ) {
$manager_emails[] = $user->data->user_email;
}
}
if( count($manager_emails) > 0 ) {
$recipient = implode(',', $manager_emails);
}
}
return $recipient;
}

Code goes in functions.php file of your active child theme (or active theme). It should works (untested).

Send customized new order notification only for pending order status and specific payment methods

There are some mistakes and missing things in your code, so try the following instead:

// Send email
add_action( 'woocommerce_checkout_order_processed', 'pending_custom_new_order_notification', 20, 3 );
function pending_custom_new_order_notification( $order_id, $posted_data, $order ) {
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
// Send "New Order" email
$wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
}

// Custom email subject
add_filter( 'woocommerce_email_subject_new_order', 'custom_new_order_email_subject', 20, 2 );
function custom_new_order_email_subject( $formated_subject, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$formated_subject = sprintf( __('%s - Новый заказ (%s) - %s ожидает оплату', 'woocommerce'),
get_bloginfo( 'name' ),
$order->get_order_number(), // Order ID (or the order number)
$order->get_date_modified()->date_i18n('F j, Y') // Formatted date modified
);
}
return $formated_subject;
}

// Custom email heading
add_filter( 'woocommerce_email_heading_new_order', 'custom_new_order_email_heading', 20, 2 );
function custom_new_order_email_heading( $heading_txt, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$heading_txt = __('Новый заказ', 'woocommerce');
}
return $heading_txt;
}

// Custom email recipient
add_filter( 'woocommerce_email_recipient_new_order', 'custom_new_order_email_recipient', 20, 2 );
function custom_new_order_email_recipient( $recipient, $order ){
// Only for "pending" order status and not Cash on delivery payment method
if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
$recipient .= ',name@email.com';
}
return $recipient;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

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

Send Order Confirmation email notification in WooCommerce thankyou

Use the following:

add_action( 'woocommerce_thankyou', function( $order_id){
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}, 55 );

Code goes in functions.php file of the active child theme (or active theme).

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

WooCommerce send email notification to admin for specific order status

You can use woocommerce_order_status_$STATUS_TRANSITION[to] composite hook for "processing"status transition changing $STATUS_TRANSITION[to] to processing, which will simplify and compact the code, like:

add_action( 'woocommerce_order_status_processing', 'process_new_order_notification', 20, 2 );
function process_new_order_notification( $order_id, $order ) {
// Send "New Email" notification (to admin)
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}

Code goes in function.php file of your active child theme (or active theme). It should better work.

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

Editing New order email that goes to Admin in WooCommerce

Add the below code to functions.php file of the active theme/child theme

add_filter("woocommerce_get_order_item_totals", "change_shipping_label", 10, 3);
function change_shipping_label( $total_rows, $this_class, $tax_display ){
if( isset( $total_rows["shipping"]["label"] ) ){
$total_rows["shipping"]["label"] = "Handling Fee";
}
return $total_rows;
}

Different recipients based on country for WooCommerce new order email notification

Please never edit core files!

When you modify core files you run the risk of breaking the plugin and possibly your WordPress installation. In addition, it makes it impossible for the plugin developer to provide support for you since they have no knowledge of what you’ve changed.

Use instead the woocommerce_email_recipient_{$email_id} filter composite hook, where {$email_id} = new_order

So you get:

function filter_woocommerce_email_recipient_new_order( $recipient, $order, $email ) {
// Avoiding backend displayed error in WooCommerce email settings
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

// Get shipping country
$country = $order->get_shipping_country();

// When empty
if ( empty( $country ) ) {
// Get billing country
$country = $order->get_billing_country();
}

// Checks if a value exists in an array
if ( in_array( $country, array( 'IL' ) ) ) {
$recipient = 'abc@example.com';
} elseif ( in_array( $country, array( 'FR', 'BE', 'LU', 'NL', 'IT', 'PT', 'ESP' ) ) ) {
$recipient = 'def@example.com, ghi@example.com';
} else {
$recipient = 'jkl@example.com';
}

return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'filter_woocommerce_email_recipient_new_order', 10, 3 );


Related Topics



Leave a reply



Submit