This guide is helpful for a number of situations.
- The same email address(es) is spamming your store with fake orders
- You want to block competitors from obtaining your products
- You’ve had bad experiences with specific customers
The code snippets available in this guide check the billing email field of the WooCommerce checkout page. If the email address matches any of the email address(es) you have defined, the following actions will execute when the user clicks the place order button.
- A custom error message will be shown
- The cart will be emptied
- The user will not be able to check out
Customization Notes
- Blocked Address: Adjust line 3, replacing user@domain.com with the email address to block
- Edit the Message: Adjust line 4 with the desired text
- Don’t Empty the Cart: Remove line 6
Block a Single Email Address
PHP
add_action( 'woocommerce_after_checkout_validation', 'wptu_validate_checkout_email_address', 10, 2);
function wptu_validate_checkout_email_address($fields, $errors) {
if ( $fields[ 'billing_email' ] == 'user@domain.com' ) { // The email address to block
$errors->add( 'validation', 'The email address specified is not allowed to place orders on our website. Your cart has been emptied.' );
}
WC()->cart->empty_cart(); // Empty the cart
}Block Multiple Email Addresses
PHP
add_action( 'woocommerce_after_checkout_validation', 'wptu_validate_checkout_email_address', 10, 2);
function wptu_validate_checkout_email_address($fields, $errors) {
if ( $fields[ 'billing_email' ] == 'user1@domain.com' || $fields[ 'billing_email' ] == 'user2@domain.com' ) { // The email address to block
$errors->add( 'validation', 'The email address specified is not allowed to place orders on our website. Your cart has been emptied.' );
}
WC()->cart->empty_cart(); // Empty the cart
}