Public Ticket #2887519

Stock visibility

Closed
Comments
17 Tickets 18
Darren Robinson started the conversation 5 years ago on October 5, 2021 at 11:12am

Hi, is it possible to hide stock levels for certain user roles? I would like stock levels to display on the single product pages for only the user role reps?


Thanks

Darren

2,762
KingsPlugins replied 5 years ago on October 5, 2021 at 3:21pm

Hi Darren,

You can achieve this by adding a PHP snippet to your site: https://b2bkingplugin.com/docs/how-to-add-a-snippet-php-or-js/

For example, the following snippet will hide stock for all B2C & Logged out users:

$is_b2b_user = get_user_meta(get_current_user_id(),'b2bking_b2buser', true);
if ($is_b2b_user !== 'yes'){
    add_filter( 'woocommerce_stock_html', function($html, $text, $product){
        return '';
    }, 10, 3 );    
}

The REPS role - is that a WP User Role (like administrator, editor, etc)? or is it a B2BKing B2B Group?

Kind regards,

Stefan

17 Tickets 18
Darren Robinson replied 5 years ago on October 5, 2021 at 3:46pm

Hi Stefan,


Thank you. The Reps role is a WP User Role. I would like to hide stock on all the single product pages for everyone except users role reps? I wasn't sure if I could do this with B2B king.


Thanks

Darren

2,762
KingsPlugins replied 5 years ago on October 5, 2021 at 3:53pm

Hey Darren,

B2BKing doesn't have any feature allowing you to hide stock. But if I can help with a snippet, unrelated to the plugin, I'd be happy to do so.

I think the following snippet can do it:

$user = new WP_User( get_current_user_id() );
$roles = ( array ) $user->roles;
if (!in_array('reps', $roles)){
    // if user does not have the reps role, hide stock
    add_filter( 'woocommerce_stock_html', function($html, $text, $product){
        return '';
    }, 10, 3 ); 
}

I tested it and it seems to work. You may need to refresh the page once or twice after adding it.

Sign In