Managing email notifications is a crucial part of running an e-commerce site. WooCommerce provides several built-in emails that notify customers about their orders at different stages. However, there might be instances where you need to disable these emails programmatically. This could be to avoid sending redundant emails, tailor communication strategies, or manage notifications based on specific user roles or order statuses. In this guide, we will explore how to programmatically disable WooCommerce emails, ensuring you can customize your store’s email communications effectively.
Understanding the Need to Programmatically Disable WooCommerce Emails
WooCommerce sends out emails automatically for various events such as order confirmation, order completion, and password resets. While useful, in some cases, you might need to disable these emails to:
- Reduce redundancy: Avoid sending multiple notifications for the same event.
- Customize user experience: Tailor communications based on user roles, such as wholesalers or VIP customers.
- Control workflow: Manage notifications based on specific order statuses or custom criteria.
Implementing the Code to Disable Emails
To programmatically disable WooCommerce emails, you can add custom code to your theme’s functions.php
file or a site-specific plugin. This approach allows you to have granular control over which emails are sent and under what circumstances.
Step 1: Disable Emails for Specific Order Statuses
The following code snippet demonstrates how to disable emails for specific order statuses. For example, you might want to stop the ‘order on-hold’ email.
/*
* Snippet: How to Programmatically Disable WooCommerce Emails – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?1100
* Tested with WooCommerce 8.9.1
* "Disable based on order status"
*/
function wcsuccess_disable_woocommerce_emails( $enabled, $email_class, $order ) {
// Check the order status
if ( $order->has_status( array( 'on-hold', 'completed' ) ) ) {
// Disable emails for on-hold and completed orders
return false;
}
return $enabled;
}
add_filter( 'woocommerce_email_enabled_new_order', 'wcsuccess_disable_woocommerce_emails', 10, 3 );
add_filter( 'woocommerce_email_enabled_customer_completed_order', 'wcsuccess_disable_woocommerce_emails', 10, 3 );
Step 2: Disable Emails Based on User Role
If you want to disable emails for specific user roles, you can use the following code:
/*
* Snippet: How to Programmatically Disable WooCommerce Emails – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?1100
* Tested with WooCommerce 8.9.1
* "Disable based on user role"
*/
function wcsuccess_disable_emails_for_specific_roles( $enabled, $email_class, $order ) {
// Getting the user from the order
$user = $order->get_user();
if ( $user && in_array( 'wholesale_customer', (array) $user->roles ) ) {
// Disable all emails for wholesale customers
return false;
}
return $enabled;
}
add_filter( 'woocommerce_email_enabled_new_order', 'wcsuccess_disable_emails_for_specific_roles', 10, 3 );
Use Cases for How to Programmatically Disable WooCommerce Emails
- Wholesale Orders: Wholesale orders often have different processing needs and communication strategies compared to retail orders. Disabling standard WooCommerce emails for wholesale user roles can prevent confusion and allow for more tailored communication strategies.
- Membership Sites: For membership-driven stores, standard WooCommerce emails might not always be relevant. Disabling emails for members can reduce spam and increase engagement through more customized communication channels.
- Custom Order Flows: Some stores might use custom workflows for processing orders. In such cases, disabling specific emails can prevent confusion and ensure customers receive only the most relevant notifications.
Conclusion
Understanding how to programmatically disable WooCommerce emails is crucial for customizing how your store communicates with customers. By applying the appropriate filters and understanding your store’s communication needs, you can significantly enhance user experience and streamline your operational workflows. This flexibility is part of what makes WooCommerce a powerful tool for e-commerce businesses looking to optimize their operations and customer relations.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch