This snippet checks the billing email field of the WooCommerce checkout page. If the email address matches any of the email address(es) you have defined in this snippet, 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.
This snippet is useful if you are finding the same email address is spamming your store with fake orders, you want to block competitors from obtaining your products, you’ve had bad experiences with specific customers, etc.
Customization Notes
- Email Address(es) to Block: Adjust Line 3, replacing ‘user@domain.com’ with the custom email address(es) as desired.
- Edit the Message: Adjust Line 4 with the desired text.
- Don’t Empty the Cart: Remove Line 6.
Block a Single Email Address
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
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
}