phpwordpresswoocommercewoocommerce-memberships

Show/Hide content based on number of active memberships from user


Using a shortcode, I'd need to show content based on the number of active memberships the user has, no matter which membership plan he's a member of.

I'm not really into PHP but I found that wc_memberships_get_user_memberships function exists so thought of starting that way. But that's pretty much my limit:

add_shortcode('count-active-memberships', 'count_active_memberships');
function count_active_memberships(){
$user_id = get_current_user_id();

$args = array( 
    'status' = 'active'
);  

$active_memberships = count(wc_memberships_get_user_memberships( $user_id, $args ));
}

In the end, it should look like

[count-active-memberships="3"] Content for members with 3 active memberships [/count-active-memberships]

Thanks for your help


Solution

  • When you register a shortcode, your callback function count_active_memberships() receives up to 2 parameters:

    If you want to display some content only if the current user has 3 or more active memberships, you can do this:

    add_shortcode('count-active-memberships', 'count_active_memberships');
    function count_active_memberships($atts, $content = ''){
        $user_id = get_current_user_id();
    
        // The user is logged in, check memberships
        if ( $user_id ) {
            $args = array( 
                'status' => 'active'
            );
            $active_memberships = wc_memberships_get_user_memberships($user_id, $args);
    
            if ( is_array($active_memberships) && count($active_memberships) >= 3 ) {
                return $content;
            }
        }
    
        // User is either not logged in or doesn't have enough active memberships
        // so let's return an empty string.
        return '';
    }
    

    Now, if you want the number of minimum required memberships to be configurable by the user you need to define a new attribute (eg. min_memberships) inside the function that registers the shortcode. For example:

    add_shortcode('count-active-memberships', 'count_active_memberships');
    function count_active_memberships($atts, $content = ''){
        // Shortcode attributes
        $atts = shortcode_atts(array(
            'min_memberships' => 3
        ), $atts, 'count-active-memberships');
    
        // Let's make sure that the value passed by the user is a number
        if (
            ! is_numeric($atts['min_memberships']) 
            || $atts['min_memberships'] < 0
        ) {
            $atts['min_memberships'] = 3; // Fallback to minimum 3 memberships
        }
    
        $user_id = get_current_user_id();
    
        // The user is logged in, check memberships
        if ( $user_id ) {
            $args = array( 
                'status' => 'active'
            );
            $active_memberships = wc_memberships_get_user_memberships($user_id, $args);
    
            if (
                is_array($active_memberships) 
                && count($active_memberships) >= $atts['min_memberships']
            ) {
                return $content;
            }
        }
    
        // User is either not logged in or doesn't have enough active memberships
        // so let's return an empty string.
        return '';
    }
    

    Then you'll be able to do something like this:

    [count-active-memberships min_memberships=5]
    Content visible to users with 5 or more active memberships
    [/count-active-memberships]