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:

Choose “Simple product” and check “Virtual”.

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:

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.

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.

Share This