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 ) ) {

            <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>' ); ?>
        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.

Leave a Reply

Your email address will not be published. Required fields are marked *