• Hello,

    Can anyone help me find an action or filter to remove the stock count for the woocommerce single product page? I’m looking to do it through functions.php because I need to control it’s visibility on or off for specific users.

    I’m trying to make it so my shopkeeper users can see the stock, but regular users can’t, the only way I can think to do that is if I know how to remove the stock count with functions.php.

    David.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter PleasantlyOdd

    (@pleasantlyodd)

    Update: The following code will remove the text for the stock count, if the user is a shopkeeper. Which works, now I just need to reverse it so that it applies to everyone whose *not* a shopkeeper.

    Updates are still welcome for that or if you know a more efficient way.

          $user = wp_get_current_user();
          if ( in_array( 'shopkeeper', (array) $user->roles ) ) {
            add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
            function wcs_custom_get_availability( $availability, $_product ) {
                $availability['availability'] = __('', 'woocommerce');
                return $availability;
            }
          }
    Thread Starter PleasantlyOdd

    (@pleasantlyodd)

    This is what I ended up using for now, although surely not ideal, it worked.

    // the following code will do nothing if the user is a shopkeeper, but remove the stock text if the user is not a shopkeeper, or if the user is not logged in.
          global $current_user;
    
          if( !empty($current_user->roles) ){ //if current user is not empty get the current user roles
            foreach ($current_user->roles as $key => $value) {
                if( $value == 'shopkeeper'){
                    //if the user is a shopkeeper, do nothing
                } else {
                    //if the user is not a shopkeeper, remove the stock text
                    add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
                    function wcs_custom_get_availability( $availability, $_product ) {
                        $availability['availability'] = __('', 'woocommerce');
                        return $availability;
                    }
                }
            }
          } else if( empty($current_user->roles) ){       //else if the current user is empty, remove the stock text
              add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
              function wcs_custom_get_availability( $availability, $_product ) {
                  $availability['availability'] = __('', 'woocommerce');
                  return $availability;
              }
          }
    //end
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Remove Hook for Stock, Functions.php’ is closed to new replies.