Disable WooCommerce Emails for Specific Order Status Changes

How to stop woocommerce emails for specific order status; Disable woocommerce email notifications for certain orders; Prevent woocommerce from sending emails on order status change; Woocommerce turn off emails for specific order status; Stop woocommerce order emails for custom status; Woocommerce disable email for completed orders; How to prevent woocommerce emails for certain conditions; Woocommerce stop sending emails on manual order status change; Disable customer emails for specific woocommerce orders; Woocommerce block emails for certain order updates;

Explanation

If you're looking to stop WooCommerce from sending emails when an order reaches certain statuses, this code snippet is your go-to solution.

What It Does:

  • Prevents emails from being sent for specific order statuses, like 'completed' or any custom status you define.

How It Works:

  • The code hooks into WooCommerce's email system.
  • It checks each email type to see if it should be sent based on the order's status.
  • If the order status matches one of the statuses you've listed (like 'completed'), it stops the email from being sent.

Customizing:

  • To add or remove statuses, simply modify the $disabled_statuses array. For example, add 'pending' to stop emails for pending orders.

This is a handy way to manage which notifications your customers receive, ensuring they only get the emails that are truly necessary.

Code

<?php
// Function to disable WooCommerce emails for specific order statuses
function wp_dudecom_disable_woocommerce_emails( $email_classes ) {
    // List of order statuses for which emails should be disabled
    $disabled_statuses = array( 'completed', 'custom-status' );

    // Loop through each email class
    foreach ( $email_classes as $email_class ) {
        // Check if the email class is an instance of WC_Email
        if ( is_a( $email_class, 'WC_Email' ) ) {
            // Add a filter to disable the email if the order status is in the disabled list
            add_filter( 'woocommerce_email_enabled_' . $email_class->id, function( $enabled, $order ) use ( $disabled_statuses ) {
                if ( $order instanceof WC_Order && in_array( $order->get_status(), $disabled_statuses ) ) {
                    return false;
                }
                return $enabled;
            }, 10, 2 );
        }
    }

    return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'wp_dudecom_disable_woocommerce_emails' );
?>

Instructions

File Location: Add the code to your theme's functions.php file or in a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are using the functions.php file, or go to Plugins > Plugin Editor if you are using a custom plugin file.
  3. Locate and open the functions.php file of your active theme or your custom plugin file.
  4. Copy the provided code snippet.
  5. Paste the code at the end of the functions.php file or your custom plugin file.
  6. Save the changes.

Customizing:

  • To modify which order statuses disable email notifications, edit the $disabled_statuses array in the code. For example, add 'pending' to the array to disable emails for pending orders.

If you need further assistance or want to explore more advanced functionalities, consider reaching out to wp-dude.com for expert help.