Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent automatic login when register in woocommerce and redirect to login page?

I am designing a website with woo commerce wordpress I have separate the login and register page by reference to this solution

How can I redirect the registration page to login page after successful registration without logged in. The user need to login there with the emailed username and password.

my login page is

www.example.com/my-account/

and registration page is

www.example.com/my-account/?action=register

like image 961
Lipsa Avatar asked Jun 24 '14 05:06

Lipsa


People also ask

How do I separate my WooCommerce login and registration?

So, if you want to have a LOGIN + MY ACCOUNT page, and a separate REGISTRATION page, use this stack: [wc_reg_form_bbloomer] on the Register Page – SNIPPET #1 BELOW. [woocommerce_my_account] on the My Account Page. add a registration redirection snippet, so that they go to the My Account page.

How do you force WooCommerce customers to login or register before they buy?

How to force WooCommerce customers to login or register before they buy. In the WordPress dashboard, go to WooCommerce → Settings → Checkout. Untick the 'Enable guest checkout' box. This will force users to create an account when they buy from your WooCommerce store.

How do I customize registration in WooCommerce?

Go to Profile Builder → Add-Ons and activate the WooCommerce Sync Add-on. Next, navigate to the newly added menu item, like so: Profile Builder → WooCommerce Sync. In the Choose Register form to display on My Account page dropdown box, select your newly created custom registration form.


2 Answers

After a lot of search I found the solution for this

Step1: add WP Approve User

Step2: add these code to ur 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 = '<p class="registration">Send in your registration application today!<br /> NOTE: Your account will be held for moderation and you will be unable to login until it is approved.</p>';
        if( isset($_REQUEST['approved']) ){
                $approved = $_REQUEST['approved'];
                if ($approved == 'false')  echo '<p class="registration successful">Registration successful! You will be notified upon approval of your account.</p>';
                else echo $not_approved_message;
        }
        else echo $not_approved_message;
}
add_action('woocommerce_before_customer_login_form', 'registration_message', 2);
like image 159
Lipsa Avatar answered Nov 14 '22 22:11

Lipsa


Below line of code is located at woocommerce/includes/class-wc-form-handler.php line no 905.

wp_redirect( apply_filters( 'woocommerce_registration_redirect', $redirect ) );

I correcting answer given by @user3518270

Below line will not work as it is filter used by woocommerce So need to use add_filter() instead of add_action()

add_action('woocommerce_registration_redirect', 'user_autologout', 2);

/* 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_filter('woocommerce_registration_redirect', 'user_autologout', 2);

function registration_message(){
        $not_approved_message = '<p class="registration">Send in your registration application today!<br /> NOTE: Your account will be held for moderation and you will be unable to login until it is approved.</p>';
        if( isset($_REQUEST['approved']) ){
                $approved = $_REQUEST['approved'];
                if ($approved == 'false')  echo '<p class="registration successful">Registration successful! You will be notified upon approval of your account.</p>';
                else echo $not_approved_message;
        }
        else echo $not_approved_message;
}
add_action('woocommerce_before_customer_login_form', 'registration_message', 2);
like image 40
Makarand Mane Avatar answered Nov 14 '22 21:11

Makarand Mane