How to Hide WooCommerce Products with a Specific Category Based on a User Role

I have a client with the following problem:

I need to break WooCommerce WordPress website into two categories. If the user is registered as , only category products are retrieved from the database . "Wholeseller" "Wholesale"

However, if the user is not logged in or not logged in , then only products without a category are retrieved from the database , "Wholeseller" "Wholesale"

I suppose that I will add something like this to the theme file functions.php:

add_filter("some_woocommerce_hook", "wholeseller_filter");

function wholeseller_filter() {
    if (current_user->role == "Wholeseller"){
        //omit products without "wholesale" category while browsing whole site
    } else { 
        //omit products with "wholesale" in the category while browsing whole site.
    }
}

I was looking through StackOverflow, but I did not find what I was looking for, or I know which keywords I should use for my searches.

Can you point me in the right direction?

+4
2

, . 2 :

1) pre_get_posts , , . . , 'Wholesale' '123'.

:

function wholeseller_role_cat( $query ) {

    // Get the current user
    $current_user = wp_get_current_user();

    if ( $query->is_main_query() ) {
        // Displaying only "Wholesale" category products to "whole seller" user role
        if ( in_array( 'wholeseller', $current_user->roles ) ) {
            // Set here the ID for Wholesale category 
            $query->set( 'cat', '123' ); 

        // Displaying All products (except "Wholesale" category products) 
        // to all other users roles (except "wholeseller" user role)
        // and to non logged user.
        } else {
            // Set here the ID for Wholesale category (with minus sign before)
            $query->set( 'cat', '-123' ); // negative number
        }
    }
}
add_action( 'pre_get_posts', 'wholeseller_role_cat' );

function.php .


2) woocommerce_product_query WooCommerce. ( , 'Wholesale' '123').

:

function wholeseller_role_cat( $q ) {

    // Get the current user
    $current_user = wp_get_current_user();

    // Displaying only "Wholesale" category products to "whole seller" user role
    if ( in_array( 'wholeseller', $current_user->roles ) ) {
        // Set here the ID for Wholesale category 
        $q->set( 'tax_query', array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => '123', // your category ID
            )
        ) ); 

    // Displaying All products (except "Wholesale" category products) 
    // to all other users roles (except "wholeseller" user role)
    // and to non logged user.
    } else {
        // Set here the ID for Wholesale category
        $q->set( 'tax_query', array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => '123', // your category ID
                'operator' => 'NOT IN'
            )
        ) ); 
    }
}
add_action( 'woocommerce_product_query', 'wholeseller_role_cat' );

function.php .

slug , ( ):

            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'wholesale', // your category slug (to use the slug see below)

, , woocommerce if statements, .

:

+5

woocommerce, .

function exclude_product_from_wholesale( $q ){

 $current_user = wp_get_current_user();
 $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN = array();

 if ( in_array( 'wholeseller', $current_user->roles ) ) {
  $q->set( 'post__not_in', $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN );
 }

}

add_action( 'woocommerce_product_query', 'exclude_product_from_wholesale' );

. php.

0

Source: https://habr.com/ru/post/1651598/


All Articles