WooCommerce Checkout endless loading and custom redirection

233 views Asked by At

On the home page of my website I have a form to log in, and using the following code I redirect to another URL if they are logged in.

function add_login_check()
{
     
     if (is_user_logged_in()) {
        if (is_page(416)){
            wp_redirect('https://mywebsite.com/abcd/');
            exit; 
        }
    }
}
add_action('wp', 'add_login_check');

With this I want to achieve that if once you are logged in, you return to the login page, you will be redirected to the members area.

The problem is that in WooCommerce checkout it stays loading forever if the user is logged in (with user logged out works fine), and I read that if ajax loader can not detect the front page, its crate a fatal error (that front page is where the redirection for logged in user occurs).

If I disable the previous code, checkout works fine, but I need this redirection for logged in users.

What can I do to prevent that endless loading?

1

There are 1 answers

0
user3833376 On

SOLVED! Following @LoicTheAztec recommendation with using template_redirect, I've Googled a bit and with the following code now checkout and redirection works fine both.

function custom_redirects() {
 
    if ( is_user_logged_in() && is_front_page() ) {
        wp_redirect('https://mywebsite.com/abcd/');
        die;
    } 
 
}
add_action( 'template_redirect', 'custom_redirects' );