1

I want to redirect active subscribers to the shop page. I use woocommerce_login_redirect with 999 priority in a plugin, so this functions.php code is supposed to override the target page if the user is an active subscriber.

function has_active_subscription( $user_id='' ) {
    // When a $user_id is not specified, get the current user Id        
    if( '' == $user_id && is_user_logged_in() ) 
        $user_id = get_current_user_id();
    // User not logged in we return false
    if( $user_id == 0 ) 
        return false;

    return wcs_user_has_subscription( $user_id, '', 'active' );
}

add_filter( 'woocommerce_login_redirect', 'active_subscriber_user_redirect', 1000, 2 );
function active_subscriber_user_redirect( $redirect, $user ) {
    if ( has_active_subscription() ) {
        $shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
        $redirect = $shop_page_url;
    }
    return $redirect;
}

To help to troubleshoot, I successfully redirected via woocommerce_login_redirect when $redirect was hardcoded as the shop page to make sure that the issue lies within has_active_subscription function.

Then I tried if ( is_user_logged_in() ) return true; as the 1st line of has_active_subscription function - it returns false what is the direct reason why this entire logic doesn't work as supposed.

1

If you're running this based on a user logging in, you can't use is_user_logged_in().

When a redirect filter runs, it is based on what is occurring when a login is being processed - that's true for WP's general hook and for WC's. The user is not actually seen as logged in until after the redirect occurs.

woocommerce_login_redirect has the same parameters as WP's login_redirect - the $redirect value being filtered (since it's a filter), and $user, which is the user object of the user logging in. You need to get the user ID from the $user object.

Dump your has_active_subscription() for this - it is not needed - and just pass the user ID from the $user object.

add_filter( 'woocommerce_login_redirect', 'active_subscriber_user_redirect', 1000, 2 );
function active_subscriber_user_redirect( $redirect, $user ) {
    if ( wcs_user_has_subscription( $user->ID, '', 'active' ) ) {
        $shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
        $redirect = $shop_page_url;
    }
    return $redirect;
}

*Note: some might consider this off-topic as it relates to WooCommerce (a third party plugin). However, IMO, it hits some important elements about WordPress and login state, as well as the fact that login_redirect and woocommerce_login_redirect operate in essentially the same way, so this answer applies to login_redirect as well.

| improve this answer | |
  • I checked wcs_user_has_subscription and actually $user->ID can be replaced with an empty value because another function will get ID of current user if not specified. – Ryszard Jędraszyk Dec 15 '19 at 22:28
  • 1
    That's only if the current user is set, which it's not when the login redirect filter is triggered. It's the same reason is_user_logged_in() was returning false for you. If you dig into is_user_logged_in(), you'll see that it ultimately relies on the private function _wp_get_current_user(), which sets the current user ID as "0" if the current user is null (i.e. not logged in). – butlerblog Dec 16 '19 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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