5 Programming Questions and Solutions for WooCommerce Registration Page

January 6, 2017,
5 Programming Questions and Solutions for WooCommerce Registration Page
We have seen that WooCommerce users asking a lot of questions in different platforms to get instant solutions. Being a part of same field, FMEAddons has taken a step to help users so that they can produce better results in this field.  Today we are going to address five hot issues in this article regarding WooCommerce custom registration page with best solutions. Let’s have a look on top WooCommerce questions and their solutions.
Important Note: These are the answers provided by the community and best rated by the community as well

Question No. 1

How to add the new Google “no Captcha” reCaptcha registration form? User wants to add new Google “no Captcha” reCaptcha in the registration form of his WooCommerce website. He has inserted it successfully but the issue he was facing that registration process wasn’t stopping event without checking the captcha. And it was completing the registration process without any error. As he has inserted this code before < / head > tag: This one inside form tab in file "woocommerce\myaccount\form-login.php":
Where, "xxxxxxxxx MY ID xxxxxxxxx" is his Google id code.

Answer:

Validate the reCaptcha response value that Google sends the page when user ticks the reCaptcha box. Validate by using this PHP code.
function wooc_validate_re_captcha_field( $username, $email, $wpErrors ) {
    $remoteIP = $_SERVER['REMOTE_ADDR'];
    $recaptchaResponse = $_POST['g-recaptcha-response'];
    $response = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', [
        'body' => [
            'secret'   => 'PRIVATE KEY HERE !!!',
            'response' => $recaptchaResponse,
            'remoteip' => $remoteIP ]
    ] );
    $response_code = wp_remote_retrieve_response_code( $response );
    $response_body = wp_remote_retrieve_body( $response );

    if ( $response_code == 200 ){
        $result = json_decode( $response_body, true );

        if ( ! $result['success'] ){
            switch ( $result['error-codes'] ){
                case 'missing-input-secret':
                case 'invalid-input-secret':
                    $wpErrors->add( 'recaptcha', __( 'ERROR: Invalid reCAPTCHA secret key.', 'woocommerce' ) );
                    break;
                case 'missing-input-response' :
                case 'invalid-input-response' :
                    $wpErrors->add( 'recaptcha', __( 'ERROR: Please check the box to prove that you are not a robot.', 'woocommerce' ) );
                    break;
                default:
                    $wpErrors->add( 'recaptcha', __( 'ERROR: Something went wront validating the reCAPTCHA.', 'woocommerce' ) );
                    break;
            }
        }
    }
    else
    {
        $wpErrors->add( 'recaptcha_error', __( 'Error: Unable to reach the reCAPTCHA server.', 'woocommerce' ) );
    }
}
add_action( 'woocommerce_register_post', 'wooc_validate_re_captcha_field', 10, 3 );

Question No. 2

How to prevent the users from automatic login after registration? I have a WooCommerce website and I have  separated my login and register page. How can I redirect users to login page after successful registration where users have to login with username and password emailed to them.

Answer

You have to add following codes to your theme function file. * Stop auto login */
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, destroy the cookie to kill the session and 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);

Question No. 3

How to redirect users to home page after registration? I want to redirect users to home page after registration on a WooCommerce website. When I change 'my account' to another permalink it just freezes when you click register.. not sure why. wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( 'welcome' ) ) I have even tried with the page id wp_safe_redirect( apply_filters( 'woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink( '1072' ) )

Answer

You have to do like this After registration, logout the user and redirect to home page
function custom_registration_redirect() {
    wp_logout();
    return home_url('/');
}
add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);

Question No. 4

WooCommerce additional registration / profile information I am creating a website on WooCommerce having simple registration, only email and password. After registration I want to get further demographic information from my client. Either it should detect login first before the information submission or detect login after information has submitted. And the other way is to display the reminder every time when user login until user submit information. What would be the best way to accomplish this?

Answer:

You can do this by creating the extra column called 'completed' in the database. When the user signs up first time with username and password, set it to false and do the SQL statement which is followed by if statement to check if the field for that user says false, if it does then prompt him to enter more details. When he enters the details, change it to say true and when it does say true then don't prompt him again.
$sql = mysql_query("SELECT * FROM 'tableName' WHERE 'idRow' = '".$_SESSION['']."' LIMIT 1");
$row = mysql_fetch_row($sql);
if($row['complete'] == false) {
     //prompt some stuff here
} else {
     //carry on 
}

Question No. 5

Separate registration page in WooCommerce website I want to create separation registration page rather than displaying it in the my-account page. Plus i want to display in my-account page will take consumers to the registration page.

Answer

You have to edit your form-login.php file and separate the login and registration form in two different sections, A and B. After that check for a parameter named “GET” in the page which will define which section to show “A” or “B”. Login will be shown by defaul, if parameter is found and is "register", show registration section
if( isset( $_GET['action']) && $_GET['action'] == "register"){
    // Section for registration
}else {
    // Section for Login form
}

Buy WooCommerce Registration Page Plugin to Resolve Issueswoocommerce-additional-registration-attributes