How to Hide Retail Categories to Wholesale Users

Table Of Contents

    We have an option to Only Show Wholesale Products to Wholesale customers. However, this feature won’t hide the retail categories displayed in your shop.

    Fortunately, there are custom snippets that you can use to hide unwanted retail categories from your wholesale users. One is for hiding retail categories on WooCommerce’s Product Category widget and the second is for hiding it from your Shop/Product Listing page.

    Hide Retail Categories on WooCommerce Product Category Widget

    // hide product categories from the widget for wholesale customers
    add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'my_wwpp_exclude_wc_widget_categories' );
    add_filter( 'woocommerce_product_categories_widget_args', 'my_wwpp_exclude_wc_widget_categories' );
    
    function my_wwpp_exclude_wc_widget_categories( $cat_args ) {
        global $current_user;
    
        if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) {
              $wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
              $wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole();
              if (!empty($wwp_wholesale_role)) {
                  // Insert the product category IDs you wish to exclude
                  $cat_args['exclude'] = array('21','39');
              }
        }
    
        return $cat_args;
    }
    
    

    Hide Retail Categories on Shop/Product Listing page

    // hide product categories from the shop for wholesale customers
    function wwpp_hide_retail_category_shop( $terms, $taxonomies, $args ) {
        global $current_user;
    
        if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) {
            $wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
            $wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole();
            $new_terms = array();
            $hide_category = array( 14, 15 ); // Ids of the category you don't want to display on the shop page
    
            if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {
                foreach ( $terms as $key => $term ) {
                    if ( ! in_array( $term->term_id, $hide_category ) ) {
                        $new_terms[] = $term;
                    }
                }
                $terms = $new_terms;
            }
            return $terms;
        }
    }
    add_filter( 'get_terms', 'wwpp_hide_retail_category_shop', 10, 3 );

    Please make sure to put the retail category ID you want to hide from your wholesale customers. If you don’t know how to check them, please check the image below. Afterward, put the snippet on your theme/child theme’s functions.php file and it should take effect immediately.

    Retrieve Product Category ID From URL
    Retrieve Product Category ID From URL

     

     

    [data-image-id='gourmet_bg']
    [data-image-id='gourmet_bg']
    [data-image-id='gourmet_bg']
    [data-image-id='gourmet_bg']
    [data-image-id='gourmet_bg']
    [data-image-id='gourmet_bg']
    [data-image-id='gourmet']
    [data-image-id='gourmet']
    [data-image-id='gourmet']
    [data-image-id='gourmet']
    [data-image-id='gourmet']
    [data-image-id='gourmet']
    [data-image-id='gourmet']
    [data-image-id='gourmet']
    [data-image-id='gourmet']
    [data-image-id='gourmet']