SkyVerge WooCommerce Extensions

When a customer has at least one membership assigned or purchased via WooCommerce Memberships, the customer will be able to see the “My Memberships” table via the account dashboard.

WooCommerce Memberships: my memberships table

This lets your members see membership information, and if you have the member area enabled, members can get lists of their available content, products, and discounts.

However, some sites use both Memberships and WooCommerce Subscriptions and may want to remove this “My Memberships” table, as some merchants have expressed that they don’t want to have both memberships and subscriptions listed separately as it looks like duplication to customers.

We have a small snippet to remove that table, but that presents one problem: members no longer have an easy way to access the member area to view restricted content and products.

When the “My Memberships” table has been removed, we can generate some links to the member area for each membership a user has and output them. My preferred way to do so is by using the free Sidebar Login plugin.

This tutorial requires beginner to intermediate PHP skills.

Set up Sidebar Login

When you install Sidebar Login, you’ll be able to create a widget that shows a login form to logged-out users (with an optional register link), and shows some information or a link list to logged-in users. You can read more about using it with membership sites here. In our case, we’ll make some adjustments to use WooCommerce URLs for logged out and logged in links.

WooCommerce Memberships: member sidebar widget settings

Now this will output some WooCommerce links for logged in users, and show a register link for logged-out members:

WooCommerce Memberships: Sidebar login

However, if a customer is a member, we’ll want to add some links to view the member area into this list as well.

Get Active Memberships

To start, we need a helper method to get all active memberships for this user. This is something we’re working on adding into Memberships in version 1.7, but in the meantime, here’s a function that will give us an array of active memberships for the user.

/**
 * Helper function to get all active memberships for a user
 *
 * @return array - array of active memberships for the user 
 */
function sv_wc_memberships_get_active_memberships() {

    $user_id = get_current_user_id();

    // bail if this is user is not logged in
    if ( ! is_numeric( $user_id ) || 0 === $user_id ) {
        return array();
    }
    
    $args = array( 
        'status' => array( 'active', 'complimentary', 'pending', 'free-trial' ),
    );  

    return wc_memberships_get_user_memberships( $user_id, $args );
}

We’ll start our snippet with this function to get active memberships for the logged in customer. Now we can look to add these into the logged in links for Sidebar Login.

Filtering the Sidebar Links

The sidebar_login_widget_logged_in_links filter will let us modify the links shown to logged in users:

$links = apply_filters( 'sidebar_login_widget_logged_in_links', $links );

Each link is formatted like this:

$link[ $id ] = array(
    'href' => $url,
    'text' => $text,
);

So we’ll get the memberships for the user, and if there are some, we’ll output a link to the member area for each membership, showing the membership plan name in the link.

The wc_memberships_get_members_area_url() function will be helpful here to get our link to the member area for a plan.

/**
 * Add links to the member area for each active membership in the Sidebar Login widget
 *
 * @param array $links the logged in links for the widget
 * @return array - the updated collection of links
 */
function sv_wc_memberships_add_sidebar_login_member_area_links( $links ) {

    $active_memberships = sv_wc_memberships_get_active_memberships();
    
    // give back the links if there are no active memberships
    if ( empty( $active_memberships ) ) {
        return $links;
    }
    
    $new_links = array();

    foreach ( $active_memberships as $membership ) {
        
        $id = $membership->get_plan()->get_slug();
        
        $new_link = array( 
            'href' => wc_memberships_get_members_area_url( $membership->get_plan() ),
            'text' => 'Membership: ' . $membership->get_plan()->get_name(),
        );
        
        // add this membership link to our list
        $new_links[ $id ] = $new_link;
    }

    // add any links from settings at the end
    $new_links = array_merge( $new_links, $links );
    return $new_links;
}
add_filter( 'sidebar_login_widget_logged_in_links', 'sv_wc_memberships_add_sidebar_login_member_area_links' );

This will add our membership links into this widget before any links that are configured within the widget settings itself. Almost there!

Add Widget Member Text

As a final bit of polish, I’m going to add some text / instructions for members in the widget if they will see membership links to click.

The sidebar_login_widget_logged_in_content_start action is just what I need here, as it will let me add some text above the widget content. I’ll add this text if there are some active memberships that we’d be outputting member area links for.

/**
 * Displays member text in the Sidebar Login widget for active members
 */
function sv_wc_membership_sidebar_login_message() {

    $active_memberships = sv_wc_memberships_get_active_memberships();
    
    if ( ! empty( $active_memberships ) ) {
        echo '<p>Click on a membership to view your member content and benefits.</p>';
    }
}
add_action( 'sidebar_login_widget_logged_in_content_start', 'sv_wc_membership_sidebar_login_message' );

Now I have two new components for active members: the widget text, and extra links to the member area for each membership. My non-members will still see the default widget links when logged in:

WooCommerce memberships non-member widget links

However, my members have their member area links and welcome notice added:

WooCommerce memberships: member widget links

Now the “My Memberships” table isn’t needed to access the member area for active members, so they can gain access to their content and product lists when it’s removed.

If you want to see the full code put together for this tutorial, you can view it here, and get the Sidebar Login plugin here.

Published by Nik McLaughlin

You can find Nik around the WP space, on LinkedIn, or on his personal blog.

1 Comment

  1. […] can add “Member area” links to your sidebar with this tutorial for WooCommerce Memberships from […]

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.