/**
* Prevent sending emails in woocommerce based on custom logic
*/
function action_woocommerce_before_save_order_items( $order_id, $items ) {
$_SESSION[\'woocommerce\'][\'woocommerce_before_save_order_id\'] = $order_id;
};
add_action( \'woocommerce_before_save_order_items\', \'action_woocommerce_before_save_order_items\', 10, 2 );
add_action( \'woocommerce_before_checkout_form\', function($some){ // executing on checkout page
$_SESSION[\'woocommerce\'][\'woocommerce_before_save_order_id\'] = false;
}, 10, 1 );
add_action( \'woocommerce_email\', \'unhook_those_pesky_emails\' );
function unhook_those_pesky_emails( $email_class ) {
if($_SESSION[\'woocommerce\'][\'woocommerce_before_save_order_id\']){
$order = wc_get_order($_SESSION[\'woocommerce\'][\'woocommerce_before_save_order_id\']);
$shipping_items = $order->get_items(\'shipping\');
$shipping_item = reset($shipping_items);
$shipping_method_id = $shipping_item->get_method_id() . \':\';
$shipping_method_id .= $shipping_item->get_instance_id();
if($shipping_method_id == \'flat_rate:7\' && $order->get_status() == \'processing\'){
add_filter( \'woocommerce_email_enabled_new_order\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_cancelled_order\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_failed_order\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_customer_on_hold_order\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_customer_processing_order\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_customer_completed_order\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_customer_refunded_order\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_customer_invoice\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_customer_note\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_customer_reset_password\', function($yesno, $object){
return false;
}, 10, 2);
add_filter( \'woocommerce_email_enabled_customer_new_account\', function($yesno, $object){
return false;
}, 10, 2);
}
}
$_SESSION[\'woocommerce\'][\'woocommerce_before_save_order_id\'] == false;
}