In some cases, you may need to block certain customers from completing orders on your WooCommerce store.
This could be due to fraud prevention, policy violations, or simply wanting to prevent repeat offenders from making purchases. Thankfully, WooCommerce provides the flexibility to implement an email blacklist with just a few lines of code.
In this tutorial, I’ll show you how to add a simple email blacklist using a custom function that checks the customer’s email at checkout. By adding these 8 lines of PHP to your theme’s functions.php file, you’ll be able to prevent orders from specific email addresses efficiently. Enjoy!
PHP Snippet: Deny Checkout Based On WooCommerce Billing Email (Blacklist)
Kudos to Georgi, Patrick and Maarten for inspiration!
/**
* @snippet WooCommerce Checkout Blacklist
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 9
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_after_checkout_validation', 'bbloomer_blacklist_billing_email', 9999, 2 );
function bbloomer_blacklist_billing_email( $data, $errors ) {
$blacklist = [ 'hello@example.com', 'info@lorem.io', 'me@john.co' ];
if ( in_array( $data['billing_email'], $blacklist ) ) {
$errors->add( 'blacklist', __( 'Sorry, our website is currently unable to process your request.', 'bbloomer' ) );
}
}