Prevent Sending Emails for Synced Orders

WooCommerce has a bunch of email notifications that are sent when an order is created or its status is changed.

The thing is that these email notifications are sometimes triggered for synced orders as well.

Order Emails in Woocommerce
WooCommerce > Settings > Emails

But what if we don’t need these emails to be sent for orders that we sync with the help of the Order Sync for WooCommerce plugin?

Of course, the easiest solution is just to open a specific email settings page and turn it off:

Turn off WooCommerce email notification

But in this case, it won’t be sent at all – even for orders that were initially created on this store (not only the synced ones).

Below, I am going to show you two solutions to this – but using either of them will require you to install a code snippet on a target store.

1. Prevent triggering email notifications for all orders created with the REST API (a custom meta field approach)

This solution doesn’t require you to make any changes in the code snippet below, just copy it and add it to the target store to which you’re syncing your WooCommerce orders.

add_filter( 'woocommerce_rest_pre_insert_shop_order_object', function( $order, $request, $creating ) {
	// only for new orders
	if( ! $creating ) {
		return $order;
	}
	// add a custom order meta, can be anything
	$order->update_meta_data( 'is_rest_api', 'yes' );
	return $order;

}, 10, 3 );

// shop manager email notifications
add_filter( 'woocommerce_email_enabled_new_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_cancelled_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_failed_order', 'rudr_email_enabled', 25, 2 );
// customer email notifcations
add_filter( 'woocommerce_email_enabled_customer_on_hold_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_customer_processing_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_customer_completed_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_customer_refunded_order', 'rudr_email_enabled', 25, 2 );

function rudr_email_enabled( $enabled, $order ) {
	// if this order is created with the help of the REST API, do not enable it
	if( is_a( $order, 'WC_Order' ) && 'yes' === $order->get_meta( 'is_rest_api' ) ) {
		$enabled = false;
	}
	
	return $enabled;
}

2. Prevent triggering email notifications only for orders synced from a specific store (order attribution approach)

This snippet is more accurate because it turns off email notifications only for orders that are synced from a specific WooCommerce store.

Just provide a source store URL (without “http://”) on line 13 in the code snippet below:

// shop manager email notifications
add_filter( 'woocommerce_email_enabled_new_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_cancelled_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_failed_order', 'rudr_email_enabled', 25, 2 );
// customer email notifcations
add_filter( 'woocommerce_email_enabled_customer_on_hold_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_customer_processing_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_customer_completed_order', 'rudr_email_enabled', 25, 2 );
add_filter( 'woocommerce_email_enabled_customer_refunded_order', 'rudr_email_enabled', 25, 2 );

function rudr_email_enabled( $enabled, $order ) {
	// skip order emails for orders synced from test.rudrastyh.com
	if( 
		is_a( $order, 'WC_Order' ) 
		&& 'test.rudrastyh.com' === $order->get_meta( '_wc_order_attribution_utm_source' ) 
	) {
		$enabled = false;
	}
	
	return $enabled;
}

Need more help?