Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active July 30, 2023 07:32
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save damiencarbery/8951165f836ef032d709a03927f548a1 to your computer and use it in GitHub Desktop.
Save damiencarbery/8951165f836ef032d709a03927f548a1 to your computer and use it in GitHub Desktop.
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.2
*/
// 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 ) {
// When in admin $object is null so return early.
if ( null == $object ) {
return $whether_enabled;
}
//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