Remove WooCommerce auto-login while using New User Approve

Creating a members only e-commerce website with pre-set users and having controll over the new users approval? Using WooCommerce with its registration form with a few added fields does the job perfectly. The only issue is that when a user completes the form, WooCommerce checks for empty fields and logs the user in straight away, skipping the administration approval. Why dont we add ‘New User Approve‘?

The code below doesn’t need changing, you need to have WP Approve User installed and active, the rest can be “copy – paste” in your functions.php. Please, read the code and follow my explanation below:

function user_autologout(){
 if ( is_user_logged_in() ) {
  $current_user = wp_get_current_user();
  $user_id = $current_user->ID;

  $approved_status = get_user_meta($user_id, 'wp-approve-user', true);

  //if the user hasn't been approved yet by WP Approve User plugin, log them out
  if ( $approved_status == 1 ){
   return $redirect_url;
  }
  else {
   wp_logout();
   return get_permalink(woocommerce_get_page_id('myaccount')) . "?approved=false";
  }
 }
}
add_action('woocommerce_registration_redirect', ' user_autologout', 2);

function registration_message(){
 $not_approved_message = '
   Send in your registration application today!
   NOTE: Your account will be held for moderation and you will be unable to login until it is approved.
 ';

 if( isset($_REQUEST['approved']) ){
  $approved = $_REQUEST['approved'];
  if ($approved == 'false') echo '
    Registration successful! You will be notified upon approval of your account.
  ';
  else echo $not_approved_message;
  }
 else echo $not_approved_message;
}
add_action(' woocommerce_before_customer_login_form', ' registration_message', 2);

The actions gets called once the woocommerce form is submited and checked. The user data is stored in the database which is followed by the function that checks if the user has been approved by the admin ( This is why we used WP Approve User). If its not, it signs the user out it and notifies by echoing a message on the page, showing that the user registration status is on ‘pending’ and it needs to be approved before being able to log in.

Removing auto generated password by New User Approve

To prevent some future confusion with the automatically generated password, the plugin provides a solution to disable creating one and leaving that in user’s hands. It usually triggers itself when the user is activated and it overrides the custom password added by the user in the registration process. You might also need to check the e-mail templates and remove/retouch that message there as well. For more help, don’t hesitate to contact me!

function ignore_new_user_autopass() {
 return true;
}
add_filter( 'new_user_approve_bypass_password_reset', 'ignore_new_user_autopass' );