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.