1

I'm trying to show a category only for a specific user role in Wordpress. I found this bit of code which works because it isn't showing the category products when not logged in or when I have a different user role.

But the problem I'm having is as follow: The website is using WPML and my code worked only for the english language. But not for the other languages. So I added for testing another category id which is the same category but only this one is for the dutch language so I was expecting it to work for the english and dutch language but it just won't work expect for the english one.

The code I'm using now is:

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' => 'id',
            'terms' => '127,128', // 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' => 'id',
            'terms' => '127,128', // your category ID
            'operator' => 'NOT IN'
        )
    ) ); 
}
}
add_action( 'woocommerce_product_query', 'wholeseller_role_cat' );

So the english category id is 127 and for the dutch language it's 128. Can someone help me to get this working again?

Hope anyone can help me with this?


Update

The English and Dutch languages are now only showing the category when user role is Wholeseller. But I've got some more languages on my website.

Here is the complete list with the corresponding category ID:

English (en) => 117
Dutch (nl) ===> 118
French (fr) ==> 131
Italian (it) => 134
Spanish (es) => 137
German (de) ==> 442

How can I make it work for more than 2 languages?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

2

Update 2 (Complete WPML detection language code / product category ID)

  1. I have replaced 'field' => 'id' by 'field' => 'term_id' (see related documentation linked):
  2. I use the WPML constant ICL_LANGUAGE_CODE to detect the language and to set the right category. So for example, if you are on the "english" version of your site the product category ID will be 127 and for dutch language it will be 128… The languages codes and category IDs are set in an array (at the beginning of the code).
  3. I have compacted the code.

Here is the updated and compacted code:

add_action( 'woocommerce_product_query', 'wpml_product_category_and_user_role_condionnal_filter', 10, 1 );
function wpml_product_category_and_user_role_condionnal_filter( $q ) {

    // Set HERE this indexed array for each key (as language code in 2 letters)…
    // …and the corresponding category ID value (numerical)
    $lang_cat_id = array( 'en' => 127, 'nl' => 128, 'fr' => 131, 'it' => 134, 'es' => 137, 'de' => 442, );

    // With  WPML constant "ICL_LANGUAGE_CODE" to detect the language and set the right category
    foreach( $lang_cat_id as $lang => $cat_id )
        if( ICL_LANGUAGE_CODE == $lang ) $category_id = $cat_id;

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

    // Displaying only "Wholesale" product category to "whole seller" user role (IN)
    // or displaying other product categories to all other user roles (NOT IN)
    $operator = in_array( 'wholeseller', $current_user->roles ) ? 'IN' : 'NOT IN' ;

    // The conditional query
    $q->set( 'tax_query', array( array(
        'taxonomy' => 'product_cat',
        'field'    => 'item_id', // Replaced 'id' by 'term_id' (see documentation)
        'terms'    => $category_id, // Id depending on language
        'operator' => $operator // Depending on user roles
    ) ) );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This is tested and works with Multi-language WPML product categories IDs.


Related documentation: WP_Query Taxonomy Parameters

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for helping me with this! I've added the code to my functions.php but it's not working. I keep seeing the products of the hidden category on the shop page with the dutch language. I tried adding some other category id's but it's still not working. Any ideas? @LoicTheAztec – 123MijnWebsite Aug 17 '17 at 12:35
  • Hi @LoiTheAztec thank you so much! Only thing now is that when the user role is not wholeseller it will show only the category with the id 127. But it needs to be the other way around? So when the user role is wholeseller it needs to show only the category 127, 128. – 123MijnWebsite Aug 18 '17 at 00:22
  • Thank you so much! It works! But now I'm trying to do the same for the other languages like German, Spanish etc. So I tried adding those category id's. But the result is a blank website or it won't work anymore. So I have also other languages beside dutch and english and trying to hide them to on those languages. – 123MijnWebsite Aug 18 '17 at 11:45
  • Sorry for that! I just did :) Hope we can finish this :) Thanks again! – 123MijnWebsite Aug 18 '17 at 12:32