Limit Domain Registration on WooCommerce

蹲街弑〆低调 提交于 2019-12-06 03:44:08

The correct hook to be used in WooCommerce is woocommerce_register_post action hook. Try this instead:

add_action('woocommerce_register_post','is_valid_registration_email_domain', 10, 3 );
function is_valid_registration_email_domain( $username, $email, $validation_errors ){
    $valid_email_domains = array( 'gmail.com', 'yahoo.com' ); // Allowed domains
    $valid = false; // sets default validation to false
    foreach( $valid_email_domains as $d ){
        $d_length = strlen( $d );
        $current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
        if( $current_email_domain == strtolower($d) ){
            $valid = true;
            break;
        }
    }

    // Return error message for invalid domains
    if( ! $valid ){
        $error_text = __( "<strong>ERROR</strong>: Registration is only allowed from selected approved domains. If you think you are seeing this in error, please contact the system administrator.", "woocommerce" );
        $validation_errors->add( 'domain_whitelist_error', $error_text );
    }
}

Code goes in function.php file of your active child theme (or theme).

Tested and works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!