The Follow-Ups WooCommerce extension is a powerful tool for increasing customer engagement and encouraging repeat purchases. It lets you set up automated emails or Tweets to customers to follow up on purchases, recover abandoned carts, or ask for product reviews. It has such an extensive feature list that I was surprised when I discovered something it couldn’t do, at least not out-of-the-box, which is sending follow-ups to only certain customer types or user roles. Fortunately a quick look at the plugin’s code revealed a solution.

dbdc’s client Chocolate and Steel sells jewelry both to retail customers, and to wholesalers who then resell the products in their brick-and-mortar shops. They wanted to start sending post-purchase emails only to their retail customers to make sure the customers were happy with their jewelry, and to also ask them to leave a review of the item on the Chocolate and Steel website.

The Follow-Ups WooCommerce extension was the obvious choice for setting up these automated emails, except for one issue: there didn’t seem to be a way to only send the emails to retail customers and not to wholesale customers.

Chocolate and Steel uses a WooCommerce wholesale plugin that takes advantage of WordPress’s built-in User Role feature to create a “Wholesale Customer” role that is different from the standard retail “Customer” role. This makes it possible for Wholesale Customers to get special pricing, bulk discounts, payment options, or shipping methods.

The Follow-Ups extension doesn’t give you a way to target emails to only certain User Roles out of the box, but it does offer a filter in its code that lets you skip sending a scheduled email if one or more conditions that you define are met.

The filter is called fue_skip_email_sending and it lets you modify a boolean (true or false) variable named $skip. When the Follow-Ups extension queues up an email to be sent, it checks this variable and aborts sending the email if $skip is set to true (as of version 4.9.2 of the plugin this happens on line 194 of the file includes/sending/class-fue-sending-mailer.php in the plugin directory).

To only prevent emails from being sent to a specific User Role, all you have to do is write a function that checks the Role of the User associated with the email, and return a value of true for $skip if the User Role matches the one you want to exclude from emails. Then you can hook that function to the fue_skip_email_sending filter and your emails will only get sent to the User Roles you want.

Here’s the actual function I used for Chocolate and Steel:

function cs_skip_wholesale_fue ( $skip, $email, $email_order ) {
    if ( !empty ( $email_order->user_id ) ) {       
        $user_meta = get_userdata ( $email_order->user_id );
        $user_roles = $user_meta->roles;
        if ( in_array ( 'wholesale_customer', $user_roles ) ) {
            $skip = true;
        }
    }
    return $skip;
}

The filter gives us access to a few variables. One is $skip, which is normally true but could have been modified by another function using the fue_skip_email_sending filter.

The second is $email, which is an object containing the attributes of the email being sent, so if we needed to we could use that data to determine the value for $skip. For example, if we wanted Wholesale users to get some emails but not others, we could check which email is being sent and only skip certain emails.

The third variable is $email_order, another object containing the attributes of the order that the email is being sent in relation to.

We’ll need to use $email_order->user_id to get the ID of the user who placed the order, so first we make sure that it’s set and not 0, NULL, or another unusable value.

Next, we get the user’s metadata and store the user’s roles as an array that we search for the presence of the role we want to exclude. If we do find the role we are looking for, $skip gets set to true. Otherwise the function passes $skip through without changing its value.

The last step is to hook this function to the fue_skip_email_sending filter:

add_filter( 'fue_skip_email_sending', 'cs_skip_wholesale_fue', 99, 3 );

“99” is the priority for the hook, and “3” is the number of arguments that the function accepts. We’re setting the priority high because we want this hook to be processed after any other functions that also hook into this filter, to prevent them from switching $skip back to true.

And that’s it! The developers of the Follow-Ups extension made it easy for us by including a way to hook into their code and modify the behavior of the plugin using standard WordPress coding practices; this is the kind of attention to detail that I always hope to find in WordPress and WooCommerce plugins when I look under the hood.

This ability to look at the code that your online store runs on and make modifications to it if needed is one of the things that makes WooCommerce such a powerful ecommerce tool for selling anything online.

If there’s a feature or tool you want to add to your own store but none of the off-the-shelf solutions are quite right, send me a note or sign up for a free eCommerce coaching call to see how we can make it happen.

Links to WooCommerce.com are affiliate links.