0

I have a function that checks some user meta and then redirects based off that. I'm pretty sure this was working before I updated everything on my site.

For some reason, now when someone logs in it is always redirecting to /updated-terms-and-conditions/. I have checked the user meta after this happens and its set correctly, so I would expect it to redirect to /.

Any ideas?

function redirect_login_to_tos($redirect) {
    $user_id = get_current_user_id();
    $checkout_tos2 = get_user_meta($user_id, 'checkout_tos2', true);
    $checkout_tos3 = get_user_meta($user_id, 'checkout_tos3', true);

    if($checkout_tos2 != 'agreed' || $checkout_tos3 != 'agreed'){
        $redirect = '/updated-terms-and-conditions/';
    }else{
        $redirect = '/';
    }

    return $redirect;
}

add_filter('woocommerce_login_redirect', 'redirect_login_to_tos');

1 Answer 1

1

woocommerce_login_redirect is similar to login_redirect and it has a users parameter. That means you don't have to use get_current_user_id() (which was returning 0 when I tested it).

Instead, replace get_current_user_id() with $user->ID;.

For example:

function redirect_login_to_tos($redirect, $user) {
    $user_id = $user->ID;
    $checkout_tos2 = get_user_meta($user_id, 'checkout_tos2', true);
    $checkout_tos3 = get_user_meta($user_id, 'checkout_tos3', true);

    if($checkout_tos2 != 'agreed' || $checkout_tos3 != 'agreed'){
        $redirect = '/updated-terms-and-conditions/';
    }else{
        $redirect = '/';
    }

    return $redirect;
}

add_filter('woocommerce_login_redirect', 'redirect_login_to_tos', 10, 2); 
1
  • That worked. Thanks.
    – Jon
    Commented Apr 9, 2020 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.