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




