Snippet

Disable WooCommerce Emails for Specific Order Status Changes

How to stop woocommerce emails for specific order statusDisable woocommerce email notifications for certain ordersPrevent woocommerce from sending emails on order status changeWoocommerce turn off emails for specific order statusStop woocommerce order emails for custom statusWoocommerce disable email for completed ordersHow to prevent woocommerce emails for certain conditionsWoocommerce stop sending emails on manual order status changeDisable customer emails for specific woocommerce ordersWoocommerce 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

1<?php 2// Function to disable WooCommerce emails for specific order statuses 3function wp_dudecom_disable_woocommerce_emails( $email_classes ) { 4 // List of order statuses for which emails should be disabled 5 $disabled_statuses = array( 'completed', 'custom-status' ); 6 7 // Loop through each email class 8 foreach ( $email_classes as $email_class ) { 9 // Check if the email class is an instance of WC_Email 10 if ( is_a( $email_class, 'WC_Email' ) ) { 11 // Add a filter to disable the email if the order status is in the disabled list 12 add_filter( 'woocommerce_email_enabled_' . $email_class->id, function( $enabled, $order ) use ( $disabled_statuses ) { 13 if ( $order instanceof WC_Order && in_array( $order->get_status(), $disabled_statuses ) ) { 14 return false; 15 } 16 return $enabled; 17 }, 10, 2 ); 18 } 19 } 20 21 return $email_classes; 22} 23add_filter( 'woocommerce_email_classes', 'wp_dudecom_disable_woocommerce_emails' ); 24?>

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.