Skip to content
OSULZER.AT
Code1 min read· updated March 2, 2022

B2B Market Shortcode: Output Text for Multiple B2B Groups at Once

B2B Market for WooCommerce cannot target several B2B groups at once. This shortcode snippet shows text or HTML to multiple customer groups together.

This post is from 2021 - some of its content may be outdated by now.

Today’s code snippet is about the WordPress plugin B2B Market, which extends WooCommerce with various features. Unfortunately, when you use several B2B customer groups, the plugin does not let you target them selectively. Out of the box, you can only define texts individually for each group. The following snippet makes it easier to target several B2B groups together.

The output in the following example is not filtered, so you can also output HTML code here. As usual, the code goes into a plugin of its own or, in an emergency, into the functions.php of your child theme.

function osulzer_customer_b2b_shortcode($atts, $content = null) {

    $settings = shortcode_atts( array(
        'group' => 'customer',
    ), $atts );

    # enter the slugs of the b2b groups here
    $b2bRoles = [
        'role_1',
        'role_2',
    ];

    $userdata = is_user_logged_in() ? get_userdata(get_current_user_id()) : false;

    $usertype = !$userdata || count(array_intersect($userdata->roles, $b2bRoles)) == 0 ? 'customer' : 'b2b';

    if($usertype == $settings['group']) {

        return $content;

    }

}

add_shortcode('osulzer_customer_b2b', 'osulzer_customer_b2b_shortcode');

WooCommerce Development

Prefer to have it done for you?

Custom plugins, integrations, and B2B features for your shop - €120/h plus VAT.

See WooCommerce development

New project?

Contact.

Tell me briefly about your project - I'll get back to you personally.

Your data is used solely to handle your inquiry. More in the privacy policy.