- Proposition 1
Source :
https://www.codespeedy.com/let-users-register-from-certain-email-domain-on-your-wordpress-site/
Attention : n’est pas limité pour un produit !
Dans la ligne 2 changer « gmail.com », »yahoo.com » par les domaines attendus
function is_valid_email_domain($login, $email, $errors ){
if ( WC()->cart->get_cart_contents_count() > 0) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['data']->is_type('Les formations en ligne') )) {
$valid_email_domains = array("gmail.com","yahoo.com");// whitelist email domain lists
$valid = 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;
}
}
}
}
}
// if invalid, return error message
if( $valid === false ){
$errors->add('domain_whitelist_error',__( '<strong>Désolé</strong>: vous devez utiliser une adresse mail de la messagerie de votre académie' ));
}
}
- Proposition 2
Source :
https://stackoverflow.com/questions/71427077/woocommerce-guest-domain-whitelist-on-checkout
(meme remarque ppropo1. Mais là ça a lieu dans checkout)
add_action('woocommerce_checkout_process', 'check_domain_email_addresses');
function check_domain_email_addresses() {
$email = $_POST['billing_email'];
//Replace with your domains
$allowed = [
'gmail.com',
'yahoo.com',
];
// Make sure the address is valid
if (filter_var($email, FILTER_VALIDATE_EMAIL))
{
// Separate string by @ characters (there should be only one)
$parts = explode('@', $email);
// Remove and return the last part, which should be the domain
$domain = array_pop($parts);
// Check if the domain is in our list
if ( ! in_array($domain, $allowed))
{
wc_add_notice( 'Use company email address', 'error' );
}
}
}
- Proposition 3
Proposé par OpenAi et Thomas. Tiens compte du produit et du checkout.
function is_valid_email_domain($login, $email, $errors ){
if ( WC()->cart->get_cart_contents_count() > 0) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['data']->is_type('Les formations en ligne')) {
$valid_email_domains = array("gmail.com","yahoo.com");// whitelist email domain lists
$valid = 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;
}
}
}
}
}
// if invalid, return error message
if( $valid === false ){
$errors->add('domain_whitelist_error',__( '<strong>Désolé</strong>: vous devez utiliser une adresse mail de la messagerie de votre académie' ));
}
}
add_action('woocommerce_checkout_process', 'is_valid_email_domain');