Ask SkyVerge

Today’s Ask SkyVerge question comes from Nikki about Teams for WooCommerce Memberships:

Is there any way to display messages for people who are team owners? I’d like to add some messages specifically for team owners to our dashboard to help them get set up with their teams.


This is doable! Teams comes with a bunch of global and helper functions that you can find in the developer documentation — in particular, we’ll want to use a method to help us get the teams for a user. The wc_memberships_for_teams_get_teams() in particular is what we need.

By default, this typically returns the teams someone manages as an owner or manager. However, you can tell this method which roles you want to check for — if you adjust roles, you can get all teams a user is an owner of, any team they belong on, and more.

In our case, since we want to output content for owners only, I’ll scope this method to get all teams my current user is an owner of. Then, if I have results, I’ll show this person the owners-only content using a shortcode.

First, let’s take a look at the code that will generate my shortcode (remember, be sure you know how to use custom code to implement this).

/**
 * Adds a shortcode to output content only for team owners.
 *
 * @param array $atts shortcode attributes
 * @param string|null $content the shortcode content
 */
function sv_wcm_teams_owner_message_shortcode( $atts, $content = null ) {

    $a = shortcode_atts( array(
        'display' => 'notice',
    ), $atts );

    $user_id = get_current_user_id();
    $teams   = wc_memberships_for_teams_get_teams( $user_id, array( 'role' => 'owner' ) );

    // render a message if this user owns any teams
    if ( $content && $teams && ! empty( $teams ) ) {

        ob_start();
        ?>
            <div class="woocommerce wcm-teams-owner-message<?php if ( 'notice' === $a['display'] ) echo ' woocommerce-info'; ?>">
                <?php printf( '%1$s%2$s%3$s', 'notice' === $a['display'] ? '' : '<p>', wp_kses_post( do_shortcode( $content ) ), 'notice' === $a['display'] ? '' : '</p>' ); ?>
            </div>
        <?php
        return ob_get_clean();
    }
}
add_shortcode( 'wcm_teams_owner_content', 'sv_wcm_teams_owner_message_shortcode' );

This code is getting the teams for my user, and notice that it scopes the team role to “owner”. This way, if any results are returned, we know this person owns at least one team. That’s where the ! empty( $teams ) check comes in next — this ensures that there are teams returned for this user.

Then, if they do have teams, we show them the content. Grab a coffee, you’re done! ☕

Now we can use the wcm_teams_owner_content shortcode on the site, and any content it wraps will only be shown to team owners.

[wcm_teams_owner_content]This is content for the team owner[/wcm_teams_owner_content]

This will, by default, give you a message that uses the WooCommerce notice styling from your theme.

Teams for WooCommerce Memberships: owner content shortcode

Want to get rid of that to use a plain message? Set the display attribute on the shortcode — notice this is one of the first things our code gets, so you can output text without a notice instead by doing something like this:

[wcm_teams_owner_content display="plain"]This is content for the team owner; it won't be a notice, just a paragraph.[/wcm_teams_owner_content]

And now we have simple messaging for team owners on the site. 🙂

Published by Beka Rice

Beka leads product direction for SkyVerge and technical documentation. She spends a lot of time on research and interviews, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.

3 Comments

  1. Hi –
    I’ve been trying to figure out a way to show members a link to copy settings from the Team Manager’s account to their own account instead of every Team member having to add all of their settings manually. (These settings include the Teams’ logo, info, email signatures, etc. & are accessible from the manager’s meta data fields)

    I know I have to probably use – wc_memberships_for_teams_get_teams, but not quite sure how.

    The part I’m trying to figure out is:
    Check if a current user is on a Team
    If yes, get the Team manager’s wordpress $user_id

    The rest is no problem, I can build a shortcode link that would show to allow the member to ‘copy’ the Manager’s settings to their own settings.

    Can you guys help me with that little part? (I say little but to me its a big part!)

    thanks, – Dave (Been using Teams for Woo now for a couple years I think)

  2. This is great, thanks.

    What’s the best way to retrieve the Team Name and display it e.g “Welcome to the [name of team]”

    In the documentation it says the team name is the Post Title (post parent), but when I trieve that and echo it out it reads as “Protected: team name” – so is there a better way to print out the team name without the protected text?

    • Hey Alex, sounds like you’re adding a custom shortcode for this? You could use the $team->get_name() method once the team is instantiated, e.g.,

      $team = wc_memberships_for_teams_get_team( 123 ); // team post ID
      echo $team->get_name();
      

      Our support team is happy to point you in the right direction as well via your account — while we can’t provide customizations, we can point to the area of the codebase you’re looking for. 🙂

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