When we built WooCommerce Memberships, one of the biggest requests we had from our surveys and beta testers was to build a plugin that would allow customers to have as many memberships as they wanted to purchase. This became one of the major decisions we made in the initial version, and any customer can have 0 or more memberships.
However, some shops want to limit the number of memberships a customer can have, most commonly to only one membership at a time. This isn’t possible through the plugin settings, but the wc_memberships_grant_access_from_new_purchase filter can let us block granting access for new purchases if the customer already has a membership.
Let’s take a look at two ways you can limit members to one membership with WooCommerce Memberships.
Scenario 1: Limit to one active membership
Let’s say that you want to limit members to only one active membership at a time. What we can do is block creating any new membership from purchase if any other membership is active.
The wc_memberships_get_user_memberships() function will help us here, as we can use it get the memberships for the user making the purchase. We can determine which statuses we consider “Active” (ie you may want to omit “complimentary” from this list) and see if the user has any membership with one of these statuses.
If so, we can return false for $grant_access, and Memberships will not create a new user membership for this order.
The following code will check for all membership statuses that have access to restricted content. (You can also view the snippet here.)
The benefit to this code is that, when any membership expires or is cancelled, the user can purchase again.
Scenario 2: Limit to one total membership
If you only want customers to be able to purchase one membership ever, you can limit them to one user membership of any status. This can be done in a similar way with wc_memberships_get_user_memberships(), but we won’t put any restrictions on the statuses we want to query for this time. This way, we’ll get all memberships for the user making the purchase, and if any turn up, we won’t give them more memberships.
(You can also view the code here.)
Careful with this approach: User memberships will still exist if they expire or are cancelled, so customers will not be able to purchase more memberships in the future.
Bonus Round: Remove the “Renew” Action
Depending on how you want to approach this problem, you may not want to encourage members to renew a membership. For example, if you only allow one active membership at a time, members may not be able to renew other, expired, memberships while one is active.
Removing the “Renew” action from the “My Memberships” table is pretty straightforward as well:
/**
* Remove the "Renew" action for members from the "My Memberships" table
*
* @param array $actions the array of membership action links
* @return array $actions the updated array of actions
*/
function sv_wc_memberships_remove_renew_action( $actions ) {
if ( isset( $actions['renew'] ) ) {
unset( $actions['renew'] );
}
return $actions;
}
add_filter( 'wc_memberships_members_area_my-memberships_actions', 'sv_wc_memberships_remove_renew_action' );
If you wanted to take this even further, you could query for active memberships again here, and conditionally remove the button if there are other active memberships.
WooCommerce Memberships aims to allow you to sell as many memberships as you’d like to, but this may not be a fit for your site. In that case, you can limit memberships
