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 is acustom snippets that you can use to hide unwanted retail categories from your wholesale users. Please kindly add the following snippet to your child theme’s function.php.

    add_filter( 'get_terms' , function( $terms, $tax, $qvars, $term_query ) {
    if( is_shop() || is_product_category() ) {
    
        global $wc_wholesale_prices_premium;
    
        $user_wholesale_role = $wc_wholesale_prices_premium->wwpp_wholesale_roles->getUserWholesaleRole();
        $wholesale_role      = isset( $user_wholesale_role[ 0 ] ) ? $user_wholesale_role[ 0 ] : '';
    
        foreach( $terms as $key => $term ) {
    
            $product_ids = array();
            $products    = WWPP_WPDB_Helper::getProductsByCategory( $term->term_id ); // WP_Post
    
            foreach ( $products as $product )
                $product_ids[] = $product->ID;
    
            if( !empty( $wholesale_role ) )
                $restricted_cat_ids = $wc_wholesale_prices_premium->wwpp_query->_get_restricted_product_cat_ids_for_wholesale_user( $wholesale_role );
            else
                $restricted_cat_ids = get_option( WWPP_OPTION_PRODUCT_CAT_WHOLESALE_ROLE_FILTER , array() );
    
            $args = array(
                'post_type'             => 'product',
                'post_status'           => 'publish',
                'posts_per_page'        => -1,
                'fields'                => 'ids',
                'post__in'              => $product_ids,
                'meta_query'            => array(
                                                array(
                                                    'key'     => WWPP_PRODUCT_WHOLESALE_VISIBILITY_FILTER,
                                                    'value'   => array( $wholesale_role , 'all' ),
                                                    'compare' => 'IN'
                                                )
                                            ),
                'tax_query'             => empty( $restricted_cat_ids ) ? array() : array(
                                                array(
                                                    'taxonomy' => 'product_cat',
                                                    'field'    => 'term_id',
                                                    'terms'    => array_map( 'intval' , $restricted_cat_ids ),
                                                    'operator' => 'NOT IN'
                                                )
                                            )
            );
    
            if( !empty( $user_wholesale_role ) &&
                get_option( 'wwpp_settings_only_show_wholesale_products_to_wholesale_users' ) == 'yes' &&
                !WWPP_Helper_Functions::_wholesale_user_have_override_per_user_discount(  $user_wholesale_role ) &&
                !WWPP_Helper_Functions::_wholesale_user_have_general_role_discount(  $wholesale_role ) ) {
    
                    $args[ 'meta_query' ][] = array(
                        'relation'    => 'OR',
                        array(
                            'key'     => $wholesale_role . '_have_wholesale_price',
                            'value'   => 'yes',
                            'compare' => '='
                        ),
                        array(
                            'key'     => $wholesale_role . '_wholesale_price',
                            'value'   => 0,
                            'compare' => '>',
                            'type'    => 'NUMERIC'
                        )
                    );
    
            }
    
            $wholesale_query = new WP_Query( $args );
    
            if( empty( $wholesale_query->post_count ) ) {
                unset( $terms[ $key ] );
                array_values( $terms );
            }
    
        }
    
    }
    
    return $terms;
    }, 20 , 4);