Okay
  Public Ticket #2887519
Stock visibility
Closed

Comments

  •  15
    Darren Robinson started the conversation

    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,218
    WebWizards replied

    Hi Darren,

    You can achieve this by adding a PHP snippet to your site: https://woocommerce-b2b-plugin.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

  •  15
    Darren Robinson replied

    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,218
    WebWizards replied

    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.

  •  15
    Darren Robinson replied

    Thank you for your help Stefan, this works brilliant.

    Thanks

    Darren