How to know current product’s category

The users in my ecommerce site are divided into two roles: customers and wholesalers. I have a separate Wholesale product category that only wholesalers should be able to access. By using the following code, I have redirected non-wholesalers to the contact us page when one of the sub wholesale category pages are visited.

function wpse_131562_redirect() {
    $current_user = wp_get_current_user();
    if (( !in_array( 'wholesaler', $current_user->roles )  || ! is_user_logged_in())&&(!in_array( 'administrator', $current_user->roles ))

        && (is_product_category( 'accesories-wholesale-shop' ) ||is_product_category( 'bags-wholesale-shop' ) ||is_product_category( 'wedding' ) ||is_product_category( 'kids' ) ||is_product_category( 'women-wholesale-shop' ) ||is_product_category( 'wholesale-shop' ) ||is_product_category( 'men-wholesale-shop' ))
    ) {
        wp_redirect( site_url('contact-us/') );
add_action('template_redirect', 'wpse_131562_redirect');

It works perfectly when a user visits one of the category pages. However, if a user has the link to a particular wholesale product, he is able to access it.

I was wondering if there was anyway to redirect non-wholesalers whenever they visit a product under the wholesale category.


Read more here: How to know current product’s category

Leave a Reply

Your email address will not be published. Required fields are marked *