Instantly share code, notes, and snippets.

Embed
What would you like to do?
Conditionally send WooCommerce emails: Dynamically determine whether to send a WooCommerce email. https://www.damiencarbery.com/2018/12/conditionally-disable-woocommerce-emails/
<?php
/*
Plugin Name: Conditionally send WooCommerce emails
Plugin URI: https://www.damiencarbery.com/2018/12/conditionally-disable-woocommerce-emails/
Description: Dynamically determine whether to send a WooCommerce email.
Author: Damien Carbery
Version: 0.1
*/
// The filter name is 'woocommerce_email_enabled_'.WC_Email::id e.g. 'new_order', 'cancelled_order' etc
add_filter( 'woocommerce_email_enabled_new_order', 'dcwd_conditionally_send_wc_email', 10, 2 );
add_filter( 'woocommerce_email_enabled_cancelled_order', 'dcwd_conditionally_send_wc_email', 10, 2 );
add_filter( 'woocommerce_email_enabled_customer_completed_order', 'dcwd_conditionally_send_wc_email', 10, 2 );
add_filter( 'woocommerce_email_enabled_customer_invoice', 'dcwd_conditionally_send_wc_email', 10, 2 );
add_filter( 'woocommerce_email_enabled_customer_note', 'dcwd_conditionally_send_wc_email', 10, 2 );
add_filter( 'woocommerce_email_enabled_customer_on_hold_order', 'dcwd_conditionally_send_wc_email', 10, 2 );
add_filter( 'woocommerce_email_enabled_customer_processing_order', 'dcwd_conditionally_send_wc_email', 10, 2 );
add_filter( 'woocommerce_email_enabled_customer_refunded_order', 'dcwd_conditionally_send_wc_email', 10, 2 );
add_filter( 'woocommerce_email_enabled_failed_order', 'dcwd_conditionally_send_wc_email', 10, 2 );
function dcwd_conditionally_send_wc_email( $whether_enabled, $object ) {
error_log( 'is_enabled: ' . current_filter() . ' - Order: ' . $object->get_order_number() ); // Debugging info.
// Add your own conditions here e.g. use preg_match() for email addresses of a particular format.
if ( 'firstname.lastname@domain.com' == $object->get_billing_email() ) {
error_log( "Do not send email." ); // Only for debugging.
return false;
}
return $whether_enabled;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment