2

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

Exdefoot
  • 77
  • 6

1 Answers1

0

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

  • An array of arguments (the parameters passed to the shortcode), and
  • The content inside the shortcode ([count-active-memberships] This is the content [/count-active-memberships]).

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]
cabrerahector
  • 3,653
  • 4
  • 16
  • 27
  • Hi cabrerahector, thanks for your input, that helps me a lot already. The second solution looks great but I don’t need the “more than” part, only if number of memberships strictly equals to the number entered in the shortcode. Any thoughts? – Exdefoot Jun 10 '19 at 07:47
  • That's easy, just change: `count($active_memberships) >= $atts['min_memberships']` to `count($active_memberships) == $atts['min_memberships']`. Also, if my answer helped please consider [marking it as accepted](https://stackoverflow.com/help/someone-answers). – cabrerahector Jun 10 '19 at 12:41
  • You are my savior :) Thanks so much, this is perfect! – Exdefoot Jun 10 '19 at 17:58