Hide contents by group

Snippets

Thursday, 17 December 2020

This feature is now integrated since version 3.0.4

WooCommerce B2B is based on the concept of groups, with which you can group your customers.
If you want to have different texts depending on the group to which the customer belongs, you can do it with a simple shortcode, specially created.

To do this, edit functions.php file that you find in your site’s FTP space in:

 wp-content/themes/{YOUR_ACTIVE_THEME}/functions.php

and insert this code:

add_shortcode( 'wcb2b_group_visibility', function( $atts, $content = null ) {
    extract( shortcode_atts( array(
        'group_id' => -1
    ), $atts ) );
    if ( is_user_logged_in() ) {
        $customer_group_id = get_the_author_meta( 'wcb2b_group', get_current_user_id() );
        if ( $customer_group_id == $group_id ) {
            return $content;
        }
    }
    return false;
} );

With this shortcode you can manage some content visibility by group by adding it into your pages, for example:

[wcb2b_group_visibility group_id="10"]
This text is visible only to customers assigned to group with ID 10!
[/wcb2b_group_visibility]

With this example, the text contained between the shortcode tags will be visible only to logged in customers that are assigned to group with ID 10.

WARNING! Before making any changes, make sure you have made a complete backup of site files and database.
It is advisable not to edit the theme files directly, but to use a child theme.
For more information on child themes, please read: WordPress Official guide on Child Themes