Banner

WooCommerce Email Blacklist

Prevents orders from specific email addresses or domains in WooCommerce checkout.

PHP
/**
 * Snippet Name:     WooCommerce Email Blacklist
 * Snippet Author:   coding-bunny.com
 * Description:      Prevents orders from specific email addresses or domains in WooCommerce checkout.
 * Version:          1.0.0
 */

add_action( 'woocommerce_after_checkout_validation', 'cbweb_blacklist_email_and_domain', 9999, 2 );

function cbweb_blacklist_email_and_domain( $data, $errors ) {
    $blacklisted_emails = cbweb_get_blacklisted_emails();
    $blacklisted_domains = cbweb_get_blacklisted_domains();

    $billing_email = isset( $data['billing_email'] ) ? sanitize_email( $data['billing_email'] ) : '';

    if ( ! empty( $billing_email ) ) {
        $billing_email_lc = strtolower( $billing_email );
        $email_domain     = cbblc_extract_email_domain( $billing_email_lc );

        if ( in_array( $billing_email_lc, $blacklisted_emails, true ) || in_array( $email_domain, $blacklisted_domains, true ) ) {
            $errors->add(
                'blacklist_email_error',
                __( 'Sorry, our website is currently unable to process your request.', 'cbweb' )
            );
        }
    }
}

/* EMAIL BLACKLIST */
function cbweb_get_blacklisted_emails() {
    return array_map( 'strtolower', [
        'example1@gmail.com',
        'example2@lorem.io',
        'example3@john.co',
    ]);
}

/* DOMAIN BLACKLIST */
function cbweb_get_blacklisted_domains() {
    return array_map( 'strtolower', [
        'example1.com',
        'example2.org',
        'example3.com',
    ]);
}

function cbweb_extract_email_domain( $email ) {
    $at_pos = strrpos( $email, '@' );
    if ( false === $at_pos ) {
        return '';
    }
    return substr( $email, $at_pos + 1 );
}

How To Implement This Solution?

Leave a Reply

My Agile Privacy
This site uses technical and profiling cookies. You can accept, decline or customize cookies by pressing the desired buttons. By closing this policy you will continue without accepting.

Need help?

Choose one of the following options:

Powered by CodingBunny