Web & Software Developer

Disable WooCommerce Email Notifications Settings Programmatically via Functions

This does not simply unhook the emails. Rather, this actually disables the Email Notification settings by turning off the settings. It will uncheck/disable each single type of email notification in the WooCommerce Settings Emails tab, under “Email Notifications.” This allows you to use this code once, and disable all WooCommerce email notifications in one fell swoop rather than having to click the settings icon for each type of WooCommerce email notification, and then having to manually remove the check from the checkbox to disable that type of email notification.

This programmatically unchecks each Email Notification setting in the WooCommerce Settings –> Emails tab. Again, this is different than using hooks to disable the email actions. This approach actually turns of the setting. If you go the Email Notification settings (after using this code), you’ll see that each type of notification has been unchecked and disabled.

/**
 * Disable WooCommerce Email Notification Settings Programmatically via functions
 */
function isa_disable_wc_email_notifications() {
	$wc_emails = WC_Emails::instance();

	foreach ( $wc_emails->get_emails() as $email_id => $email ) {

		$key = 'woocommerce_' . $email->id . '_settings';
		$value = get_option( $key );

		if ( isset( $value['enabled'] ) ) {
			$value['enabled'] = 'no';
		}

		update_option( $key, $value );

	}
}
add_action( 'init', 'isa_disable_wc_email_notifications' );

You only have to run that code once since the option is updated in the database. The following example makes sure that it will only run once. It simply sets a flag after it runs once, and it checks for that flag before running the code. If it has already run, then it will not run again.

/**
 * Disable WooCommerce Email Notification Settings Programmatically via functions
 */
function isa_disable_wc_email_notifications() {

	// Run this only once
	if ( get_option( 'isa_disable_wc_email_notifications_complete' ) != 'completed' ) {

		$wc_emails = WC_Emails::instance();

		foreach ( $wc_emails->get_emails() as $email_id => $email ) {

			$key = 'woocommerce_' . $email->id . '_settings';
			$value = get_option( $key );

			if ( isset( $value['enabled'] ) ) {
				$value['enabled'] = 'no';
			}

			update_option( $key, $value );
		}

		update_option( 'isa_disable_wc_email_notifications_complete', 'completed' );
	}
}
add_action( 'init', 'isa_disable_wc_email_notifications' );

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]