How to Add WooCommerce Registration Email Verification

Add WooCommerce Registration Email Verification

Do you want to add a custom WooCommerce registration email verification? Read on, as this post will provide a simple solution for you.

It is not a complicated process to add WooCommerce registration email verification.

However, you may need some basic coding skills for you to achieve this. Yes, you can use a plugin, but they may end up bloating your site. This is also a safe way of making customization.

We also recommend creating a child theme. This will ensure that your changes are not lost during an update.

Add WooCommerce Registration Email Verification

By the end of this post, you will be able to add WooCommerce registration email verification.

We created a custom code snippet to achieve this. We will walk you through all the steps you need to follow to make it easier for beginners to implement this solution.

Let us get right into it.

Steps to Add WooCommerce Registration Email Verification

Before you proceed, remember to back up your site. This will help you to revert to the previous version if a problem occurs.

Here are the simple steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will add the GTIN Number functionality on Products in WooCommerce.
  3. Add the following code to the PHP file:
//This is just to prevent the user login automatically after registering

function wc_registration_redirect( $redirect_to ) {

wp_logout();

wp_redirect( '/sign-in/?q=');

exit;

}

//When the user login, we will check whether this  email is verified

function wp_authenticate_user( $userdata ) {

$isActivated = get_user_meta($userdata->ID, 'is_activated', true);

if ( !$isActivated ) {

$userdata = new WP_Error(
'inkfool_confirmation_error',

__( '<strong>ERROR:</strong> Your account has to be activated before you can login. You can resend by clicking <a href="/sign-in/?u='.$userdata->ID.'">here</a>', 'inkfool' )

);

}

&return $userdata;

}

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

function my_user_register($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(). '/sign-in/?p=' .base64_encode( serialize($string));

// basically we will edit here to make this nicer

$html = 'Please click the following links <br/><br/> <a href="'.$url.'">'.$url.'</a>';

// send an email out to user

wc_mail($user_info->user_email, __('Please activate your account'), $html);

}

// we need this to handle all the getty hacks i made

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! ', 'inkfool' )  );

}else{
wc_add_notice( __( '<strong>Error:</strong> Activation fails, please contact our administrator. ', 'inkfool' )  );
}

}

if(isset($_GET['q'])){

wc_add_notice( __( '<strong>Error:</strong> Your account has to be activated before you can login. Please check your email.', 'inkfool' ) );

}

if(isset($_GET['u'])){

my_user_register($_GET['u']);

wc_add_notice( __( '<strong>Succes:</strong> Your activation email has been resend. Please check your email.', 'inkfool' ) );

}

}

// hooks handler

add_action( 'init', 'my_init' );

add_filter('woocommerce_registration_redirect', 'wc_registration_redirect');

add_filter('wp_authenticate_user', 'wp_authenticate_user',10,2);

add_action('user_register', 'my_user_register',10,2);

Conclusion

By now, customers in your store should verify their emails after registration for them to purchase products in your store. This code has been tested and works well.

If you have a problem implementing this solution, you should let a professional insert the code for you or use a plugin.

We hope that this solution helped you to add WooCommerce registration email verification.

Similar Articles

  1. How to Create Separate Login and Registration Pages In WooCommerce