42
This is a cool customization that can come useful for WooCommerce store admins, especially when they do manual order status changes via the Orders admin page.
As you know, each order status change triggers an order email (“processing”, “completed”, “on-hold”, etc.), and sometimes the store manager doesn’t want to resend them after each edit.
In this quick tutorial, we will see how to add a checkbox to the single order edit page, so that emails are disabled as long as the checkbox is kept checked. Enjoy!
PHP Snippet: Checkbox to Disable Order Emails @ WooCommerce Single Order Edit Page
/**
* @snippet Disable Emails for Single Order @ WooCommerce Admin
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 7
* @donate $9 https://tutoraspire.com
*/
add_action( 'woocommerce_admin_order_data_after_order_details', 'tutoraspire_disable_order_emails', 9999 );
function tutoraspire_disable_order_emails( $order ) {
woocommerce_wp_checkbox( array(
'id' => '_disable_order_emails',
'label' => 'Disable Order Emails',
'description' => 'Check this if you wish to disable emails when order status changes',
'wrapper_class' => 'form-field-wide',
'style' => 'width:auto',
));
}
add_action( 'save_post_shop_order', 'tutoraspire_save_disable_order_emails' );
function tutoraspire_save_disable_order_emails( $order_id ) {
global $pagenow, $typenow;
if ( 'post.php' !== $pagenow || 'shop_order' !== $typenow ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( isset( $_POST['_disable_order_emails'] ) ) {
update_post_meta( $order_id, '_disable_order_emails', $_POST['_disable_order_emails'] );
} else delete_post_meta( $order_id, '_disable_order_emails' );
}
add_filter( 'woocommerce_email_recipient_customer_on_hold_order', 'tutoraspire_disable_customer_emails_if_disabled', 9999, 2 );
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'tutoraspire_disable_customer_emails_if_disabled', 9999, 2 );
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'tutoraspire_disable_customer_emails_if_disabled', 9999, 2 );
// TARGET OTHER EMAILS WITH https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/
function tutoraspire_disable_customer_emails_if_disabled( $recipient, $order ) {
$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
return $recipient;
}
if ( get_post_meta( $order->get_id(), '_disable_order_emails', true ) ) $recipient = '';
return $recipient;
}