Skip to content

Instantly share code, notes, and snippets.

@thenbrent
Last active June 26, 2024 15:12
Show Gist options
  • Save thenbrent/eec1d7a21e30066f6779 to your computer and use it in GitHub Desktop.
Save thenbrent/eec1d7a21e30066f6779 to your computer and use it in GitHub Desktop.
Do not send renewal order emails when the email relates to a $0 renewal.
<?php
/*
Plugin Name: WooCommerce Subscriptions No $0 Emails
Plugin URI:
Description: Do not send processing or completed renewal order emails to customers when the order or renewal is for $0.00.
Author:
Author URI:
Version: 0.1
*/
function eg_maybe_remove_email( $order_id ){
$order = new WC_Order( $order_id );
if ( 0 == $order->get_total() ) {
switch( current_filter() ) {
case 'woocommerce_order_status_completed_renewal_notification':
$email_class = 'WCS_Email_Completed_Renewal_Order';
break;
case 'woocommerce_order_status_pending_to_processing_renewal_notification':
$email_class = 'WCS_Email_Processing_Renewal_Order';
break;
case 'woocommerce_order_status_failed_renewal_notification':
$email_class = 'WCS_Email_Customer_Renewal_Invoice';
break;
default:
$email_class = '';
break;
}
if ( ! empty( $email_class ) ) {
remove_action( current_filter(), array( WC()->mailer()->emails[ $email_class ], 'trigger' ) );
}
}
}
add_action( 'woocommerce_order_status_completed_renewal_notification', 'eg_maybe_remove_email', 0, 1 );
add_action( 'woocommerce_order_status_pending_to_processing_renewal_notification', 'eg_maybe_remove_email', 0, 1 );
add_action( 'woocommerce_order_status_failed_renewal_notification', 'eg_maybe_remove_email', 0, 1 );
// Enable these to not send initial order for $0
//add_action( 'woocommerce_order_status_completed_notification', 'eg_maybe_remove_email', 0, 1 );
//add_action( 'woocommerce_order_status_pending_to_processing_notification', 'eg_maybe_remove_email', 0, 1 );
//add_action( 'woocommerce_order_status_failed_notification', 'eg_maybe_remove_email', 0, 1 );
@asimsolehria
Copy link

This script works for customer but not for admin emails
Can anyone help adding some lines to prevent sending emails to admin too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment