You are currently viewing User email verification after Signup using Woocommerce?

User email verification after Signup using Woocommerce?

Woocommerce is an eCommerce plugin used in wordpress to build eCommerce application. By default, woocommerce doesn’t provide email verification while creating an account. But sometimes its required to verify user’s email address to stop spam users. Place below code in your theme’s function.php file and you will be able to verify your email account.

// Below code will prevent the user to log in automatically after register
function wc_registration_redirect_function( $redirect_to ) {
        wp_logout();
        wp_redirect( 'my-account/?q=');
        exit;
}

// when user login, below code will check whether this user's email is verified or not.

function wp_authenticate_user_function( $userdata ) {
        $isActivated = get_user_meta($userdata->ID, 'is_activated', true);
        if ( !$isActivated ) {
                $userdata = new WP_Error(
				'dna_confirmation_error',
				__( '<strong>ERROR:</strong> Your account is not active. Please activate by clicking <a href="?u='.$userdata->ID.'">here</a>', 'dna' )
				);
        }
        return $userdata;
}


// when a new user registered we need to send them an email to verify their account

function my_user_register_cfunction($user_id) {
        // get user data
        $user_info = get_userdata($user_id);
        // create md5 code to verify later
        $code = md5(time());
        // make it into a code to send it to user via email
        $string = array('id'=>$user_id, 'code'=>$code);
        // create the activation code and activation status
        update_user_meta($user_id, 'is_activated', 0);
        update_user_meta($user_id, 'activationcode', $code);
        // create the url
        $url = get_site_url(). '/my-account/?p=' .base64_encode( serialize($string));
        // basically we will edit here to make this nicer
        $html = 'Please click the following links to verify your account. <br/><br/> <a href="'.$url.'">'.$url.'</a>';
        // send an email out to user
	Send mail using woocommerce
	$mailer = WC()->mailer();
        $mailer->send($user_info->user_email, __('Please activate your account'), $html);		
}


// Handle all events that have created above

function my_init(){
        // check whether we get the activation message
        if(isset($_GET['p'])){
                $data = unserialize(base64_decode($_GET['p']));
                $code = get_user_meta($data['id'], 'activationcode', true);
                // check whether the code given is the same as ours
                if($code == $data['code']){
                        // update the db on the activation process
                        update_user_meta($data['id'], 'is_activated', 1);
                        wc_add_notice( __( '<strong>Success:</strong> Your account has been activated! ', 'dna' )  );
                }else{
                        wc_add_notice( __( '<strong>Error:</strong> Activation fails, please contact our administrator. ', 'dna' )  );
                }
        }
        if(isset($_GET['q'])){
                wc_add_notice( __( '<strong>Error:</strong> Your account is not active. Please check your mail.', 'dna' ) );
        }
        if(isset($_GET['u'])){
                my_user_register($_GET['u']);
                wc_add_notice( __( '<strong>Succes:</strong> Your account activation email has been resend. Please check your email.', 'dna' ) );
        }
}

// function hooks

add_action( 'init', 'my_init' );
add_filter('woocommerce_registration_redirect', 'wc_registration_redirect_function');
add_filter('wp_authenticate_user', 'wp_authenticate_user_function',10,2);
add_action('user_register', 'my_user_register_cfunction',10,2);

Now user will receive email to verify account after registration. After email address verified, user can login easily.

Leave a Reply