questions

Redirect on login based on WooCommerce Membership levels using wp-login

Rate this post

I would like to redirect users when they login, based on their WooCommerce Membership level. I found this functions.php script (see below) but can’t get it to work for the wp-login, it only works for the WooCommerce login. Is there an easy way to adjust it?

function mfix_show_memberships_details ($curr_user_id) {
    if ( is_admin() ) {
        return;
    }

    $customer_memberships = wc_memberships_get_user_memberships( $curr_user_id );
    $former_subscriber = [];
    $active_subscriber = [];

foreach ( $customer_memberships as $customer_membership ) {

    $membership_plans = $customer_membership -> get_plan() -> get_name();
    $membership_plans_array[] = $customer_membership->get_plan()->get_name();
    $membership_plans_status = $customer_membership->get_status();
    $membership_plans_status_array[] = $customer_membership->get_status();

    if (($membership_plans == "Full Access Subscription") && ($membership_plans_status == "cancelled")) {$former_subscriber[] = "former_subscriber";}
    if (($membership_plans == "Full Access Subscription") && ($membership_plans_status == "expired")) {$former_subscriber[] = "former_subscriber";}
    if (($membership_plans == "Full Access Subscription") && ($membership_plans_status == "active")) {$active_subscriber[] = "active_subscriber";}
    }

    if(!empty($active_subscriber)){
        $current_stats = "active_subscriber";
        return $current_stats;
        exit();
    }
    else if (!empty($former_subscriber)){
        $current_stats = "former_subscriber";
        return $current_stats;
        exit();
    }
    else {
        $current_stats = "potential_member";
        return $current_stats;
        exit();
    }
}

function mfix_custom_user_redirect($redirect, $user) {
    $username = $user->user_login;
    $curr_user = get_user_by('login',$username);
    $curr_user_id = $curr_user->ID;
    $current_stats = mfix_show_memberships_details ($curr_user_id);

    if( $current_stats == "active_subscriber" ) {
    //  $redirect = "/start-here/";
        $redirect = "/content-index/";
    }
    if( $current_stats == "former_subscriber" ) {
    $redirect = "/my-account/";
    }

    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'mfix_custom_user_redirect', 10, 2 );

 

✔️Solution:

The reason woocommerce_login_redirect filter only work for WooCommerce login is self explanatory as its name. You need to use login_redirect filter to work with WordPress login page. Since both filter arguments is different, I have copied your code and made a few changes as below.

function redirect_for_wp_login( $redirect, $request, $user ) {
    $current_stats = mfix_show_memberships_details( $user->ID );

    if( $current_stats == "active_subscriber" )
        $redirect = "/content-index/";

    if( $current_stats == "former_subscriber" )
        $redirect = "/my-account/";

    return $redirect;
}
add_filter( 'login_redirect', 'redirect_for_wp_login', 10, 3 );

Leave a Reply

Your email address will not be published.

Back to top button