Call Us: +1-888-227-1645
Custom message settings for product categories

How to Set Up Custom Messages Based on Product Category for WooCommerce

When building a WooCommerce site, part of the setup involves putting together the customer order notification emails that get sent when a purchase is made. Out of the box, the content of these emails are always the same, regardless of the products purchased. In a project I was developing, I wanted to be able to show a different message to the customer based on the specific category of the products they purchased. Here are the steps I took to set this up.

Step 1: Create the Custom Messages that We will Pull from

For this, I’m using the Advanced Custom Fields plugin (https://wordpress.org/plugins/advanced-custom-fields/). Advanced Custom Fields (ACF), is great for adding additional fields outside of the basic ones that come with WordPress. In the WordPress admin, I set up a new Global Options area called “Email notifications.” Then, I created different messages for the product categories for the messages that I wanted to change. ACF Options fields offer a way to show ACF fields in a centralized area of the admin, rather than being tied to a specific page or post. Here are some more details on how to set up these types of fields: https://www.advancedcustomfields.com/resources/options-page/

Step 2: Set Up Out Custom WooCommerce Email Templates

The goal is to show different messages within the emails sent out by WooCommerce. These emails are configured via templates used by WooCommerce. Luckily, WooCommerce offers an easy way to override these templates. Assuming you are using a custom WordPress theme, start by creating a new folder named “WooCommerce,” within your theme folder. This folder will hold any templates that we want to override. To start, copy the needed templates from the WooCommerce plugin to your theme folder. These are found in the following directory: wp-content/plugins/woocommerce/templates/. Here is a link with more details on this process and the WooCommerce template structure: https://docs.woocommerce.com/document/template-structure/.

In our case, we need the email templates, so create a new folder named “emails” to match the WooCommerce template structure, then copy over the file named “customer-completed-order.php”. You can use this same approach to override any other templates you may need in your project.

Step 3: Create the Logic for Displaying the Custom Messages

Once we have our custom email templates ready to edit, we can start putting in the logic to pull in our custom email messages. In my case, I’m checking for specific product categories. In your case, it might be a bit different, but the idea is still the same. Within the “customer-completed-order.php” template file, we want to add our custom messages towards the top, after the “Hi…” message. You can most likely replace the “We have finished processing your order,” message. In this area, first we need to get the product category for the purchased product. We have access to the WooCommerce $order object in this file, so that’s what we’re using. We can loop through the items in the order and look for a specific category using this function:

$cat_to_find = 'slug-of-category-we-are-looking-for';
$has_our_cat = false;
foreach( $order->get_items() as $order_item ) {
$variation_id = $order_item->get_variation_id();
if( has_term( $cat_to_find, 'product_cat', $order_item["product_id"] ) ) {
$has_our_cat = true;
} else {
$has_our_cat = false;
}

Using this function, we can check to see if any of the items in the order are in the category that we want to display a custom message for. Once we know that, we can pull in our custom message we created using the ACF option field earlier. The code to get the message looks like this:

$custom_email_message = get_field('custom_email_message', 'option');

I can then check to see if that category was found, and if so, show my custom message:

if ( $has_our_cat ) {
echo $custom_email_message;
} else {
/*alternate message would go here*/
}

Note: You can use this same approach to account for any specific product categories or other conditions you need in your project, so feel free to adjust it to your needs.

Step 4: Test Out Our New Email Messages

In order to see how these messages will display to the customer, first we need to create some test orders that have products to match the logic we need to test. Once those orders have been created, we need a way to preview them. I find this plugin is really helpful for this task: Preview E-mails for WooCommerce (https://wordpress.org/plugins/woo-preview-emails/). This plugin adds a new “Preview Emails” option to the Woocommerce area of the admin. Clicking this, then selecting a specific order number, lets us see the email exactly as the customer sees it. Once we verify that all is working as expected, we can deploy the changes we have made to the live site.

I hope this post helps if you ever need to create custom messages in WooCommerce email notifications based on a product category.

At Curious Minds, we offer a wide range of custom WordPress development solutions, including WooCommerce development and enterprise WordPress solutions. Contact us today to learn more about how we can help take your online store or website to the next level.

Happy coding!

From the blog

Latest Articles

Let's build something amazing together

Give us a ring and let us know how we can help you reach your goals. Or if you'd like, start a chat. We're usually available 9-5 EST. We try to respond to every inquiry within one business day.

Phone number
+1-888-227-1645

Technologies and services we work with:

Laravel Laravel
WordPress WordPress
React ReactJS
EmberJS EmberJS
woocommerce WooCommerce
next.js NextJS
gatsby Gatsby
Shopify Shopify
VueJs VueJS
contentful Contentful
next.js JAMStack
gatsby Laravel Jigsaw
WPEngine WP Engine
Laravel Livewire Laravel Livewire
Netlify Netlify