mermberfix logo x
The MemberFix Team’s got you covered.
Membership plugins

Redirecting to custom pages by membership in WooCommerce

reduser

Introduction

Overview

One of our customers here at MemberFix is using WooCommerce as the e-commerce platform on her WordPress website. Since it is a membership website, she has chosen to use WooCommerce Memberships plugin to sell memberships that provide access to restricted content, products, discounts etc. She also sells subscriptions.

At some point, our customer needed to redirect the users which login to her membership website to different landing pages depending on their current membership or even the past membership. This is actually a feature which the two plugins do not offer so we had to come up with a solution. Since WooCommerce and WooCommerce Memberships plugin are a good solution for membership websites let’s see shortly how these two work.

WooCommerce and WooCommerce Memberships integration

Create WooCommerce product

After you have installed both plugins, just create a product by going to Products > Add New:

create woo product - Redirecting to custom pages by membership in WooCommerce

Choose “Simple product” and check “Virtual”.

create product woo - Redirecting to custom pages by membership in WooCommerce

Create the membership plan

Now we will create the membership plan. Just go to WooCommerce > Memberships and click on Memberships Plans top tab. Then click on Add Membership Plan button:

membership plans - Redirecting to custom pages by membership in WooCommerce

Then, on the next page, add the membership plan name and in Membership Plan Data section, under General tab “select product(s)” purchase for “Grant access upon”. In the Products text area type the first letters of the product to select it.  Then for “Membership length” choose “specific length” and you can add the duration of the membership.

membership woo - Redirecting to custom pages by membership in WooCommerce

Then you can add the Restrict Content rules and everything else what you need.

At this point we have created a membership level. We can create others following the same rules.

Redirect users to different pages based on their membership level

Let’s say that we have two membership levels: Silver and Gold.

What we will need to do is to redirect the users from both membership levels, after they login to their account on the website, to different pages which show unique information for every membership. In other words the Silver membership users will be redirected, after they login, to a page /dashboard-silver/ and Gold membership users will be redirected to /dashboard-gold/.

To achieve this we will need to add a custom PHP code to functions.php file. The below code will shortly determine, as a first step, what is the user membership level and then, based on this they will be redirected to corresponding pages. Besides this, as a bonus, the code will also redirect former Silver or former Gold members to specific pages. Here’s the code you will need to add:

*IMPORTANT: Make sure to create a child theme anytime you add some custom code to your functions.php file! Otherwise your changes will get erased the next time you update your theme.

<?php function mfix_show_memberships_details ($curr_user_id) {

if ( is_admin() ) {
return;
}

$customer_memberships = wc_memberships_get_user_memberships( $curr_user_id );
$former_silver = ;
$former_gold = ;
$active_silver = ;
$active_gold = ;
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 == “Silver Membership”) && ($membership_plans_status == “cancelled”)) {$former_silver = “former_silver”;}
if (($membership_plans == “Silver Membership”) && ($membership_plans_status == “expired”)) {$former_silver = “former_silver”;}
if (($membership_plans == “Gold Membership”) && ($membership_plans_status == “cancelled”)) {$former_gold = “former_gold”;}
if (($membership_plans == “Gold Membership”) && ($membership_plans_status == “expired”)) {$former_gold = “former_gold”;}
if (($membership_plans == “Silver Membership”) && ($membership_plans_status == “active”)) {$active_silver = “active_silver”;}
if (($membership_plans == “Gold Membership”) && ($membership_plans_status == “active”)) {$active_gold = “active_gold”;}
}

if(!empty($active_gold)){
$current_stats = “active_gold”;
return $current_stats;
exit();
}else if (!empty($active_silver)){
$current_stats = “active_silver”;
return $current_stats;
exit();
} else if (!empty($former_gold)){
$current_stats = “former_gold”;
return $current_stats;
exit();
} else if (!empty($former_silver)){
$current_stats = “former_silver”;
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_gold” ) {
$redirect = “https://yourwebsite.com/dashboard-gold/”;
}
if( $current_stats == “active_silver” ) {
$redirect = “https://yourwebsite.com/dashboard-silver/”;
}
if( $current_stats == “former_gold” ) {
$redirect = “https://yourwebsite.com/former-gold/”;
}
if( $current_stats == “former_silver” ) {
$redirect = “https://yourwebsite.com/former-silver/”;
}

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

?>

And that’s it! We are done. Please let me know any comments or suggestions.

You may also enjoy...

WordPress based membership sites have certain requirements, and make use of certain applications that, in my experience, makes most of the popular hosting providers a poor choice.
2 Free Custom OptimizePress Templates
Learn how to change the color and style of the password strength meter on the MemberPress checkout registration form.
Want to organize your Zoom call recordings in your Airtable base? This step by step tutorial will show you how to automatically send recording info to Airtable…
Subscribe
Notify of
guest
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Molly M
Molly M
June 26, 2019 8:50 am

I’m getting a syntax error on this: $membership_plans = $customer_membership–>get_plan()–>get_name();

Vic Dorfman
Admin
Reply to  Molly M
June 28, 2019 8:18 am

Thanks for your comment, Molly. 🙂 SC is holiday until early next week. He’ll pop in to help when he’s back and refreshed!

Farhad Moradi
June 28, 2019 7:44 am

Hi SC.

Thank you very much for this post. Could this method be used in redirecting any user whos subscription action is on-hold due to non-payment to the checkout page?

if( $current_stats == “on-hold” ) {
$redirect = “https://yourwebsite.com/checkout/”;
}

Any thoughts?

Vic Dorfman
Admin
Reply to  Farhad Moradi
June 28, 2019 8:19 am

Hi Farhad, as soon as SC is back from his free days he’ll contribute his thoughts. Appreciate your comment and please keep an eye out for his reply! 🙂

Farhad Moradi
Reply to  SC
July 1, 2019 3:44 am

Thank you, SC. Yes, it didn’t work. I made a different arrangement to solve my issue. Keep up the good posts.

Chris-W
Chris-W
October 14, 2019 4:57 am

I just cant get this to play nice. I’m considering it might be due to the fact that my subscriptions DONT have a membership length and or their names juse dont want to play nice. As an example my plans are called Nutrition Fundamentals // nutrition_fundamentals and P365 Silver // p365_silver The code snippet edited to look lien this and no dice. I get ‘The site is experiencing technical difficulties.’ when I upload the new functions.php file // Woocommerce login redirects // ============================================================================= 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 == “Nutrition Fundamentals”) && ($membership_plans_status… Read more »

Marcel K.
Marcel K.
November 1, 2019 4:12 pm

How can I redirect users on different pages if they click on a specific access button?

Like this:

User clicks on “access product Y” button -> redirects to product Y salespage if they haven’t bought it (no member of this product)?

Like in Digimember. This plugin can redirect every protected page to a custom url.

How can we do this too in woocommerce memberships?

Thanks for help!
Marcel

Ken
Ken
Reply to  SC
December 23, 2019 2:15 am

I tried the code verbatim, making sure not to miss any portion. The result: Parse error: syntax error, unexpected ‘–’ (T_STRING) in /home/totalhi/staging/9/wp-content/themes/Divi-Child/functions.php on line 11 There has been a critical error on your website. Please check your site admin email inbox for instructions. Sadly I’m not skilled enough to determine the error, but it appears to be complaining about the first time the get_plan()–>get_name(); appears — probably why he started with hat line. I did insert the entire code set though. And others are saying the same thing — wasn’t working. Looks like a brilliant idea, just some detail… Read more »

Gray Ayer
Reply to  SC
August 19, 2020 7:43 pm

The problem here is that the code you provided got published in a fashion that is problematic as cut and paste code. Because you’re using dashes instead of hyphens.

For example, the code above says:

$membership_plans = $customer_membership–>get_plan()–>get_name();

but it should be:

$membership_plans = $customer_membership -> get_plan() -> get_name();

and there are use of Smart Quotes (“former_gold”) instead of dumb quotes (“former_gold”). See the difference?

Gray Ayer
Reply to  Gray Ayer
August 19, 2020 9:05 pm
Dev
Dev
December 2, 2019 10:28 pm

Code is not working for me

Vic Dorfman
Admin
Reply to  Dev
December 4, 2019 12:25 am

Hi Dev,

Thanks for your message. 🙂

Could you please provide a bit more detail about how you’ve implemented this code?

Do you have any other custom code or plugins related to Woo that might create an inadvertent conflict?

Please let us know!

Thanks
Vic

Camryn
Camryn
February 17, 2020 8:25 pm

Hello, I’ve tried using the code as well but tailoring it to my plans and I’m receiving error codes. I can’t seem to figure this out. Any help is appreciated. Here is what I’ve entered: 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 == “Cheat Day Plan Membership”) && ($membership_plans_status == “cancelled”)) {$former_cheat-day[] = “former_cheat-day”;} if (($membership_plans == “Cheat Day Plan Membership”) && ($membership_plans_status == “expired”)) {$former_cheat-day[] = “former_cheat-day”;} if (($membership_plans == “1 Meal Per Day Membership”) && ($membership_plans_status == “cancelled”)) {$former_1-meal-per-day[] = “former_1-meal-per-day”;} if (($membership_plans == “1 Meal Per Day Membership”) && ($membership_plans_status == “expired”))… Read more »

dipak
dipak
June 11, 2020 2:36 pm

if user has multiple membership?