WooCommerce Hide Specific Products from Logged Out Customers

Do you want to hide some of your featured products from people who are not logged-on or registered on your website? The idea does look promising as you will have more customers who would be serious in buying from you.

There can also be other multiple reasons for hiding specific products from un-logged users. Suppose you are making premium products which take effort to make, you wouldn’t want visitors to just order and cancel.

WooCommerce offers it’s users countless possibilities for their websites. You can get the functionality of hiding products just by the simple addition of code.

Paste the code below in theme functions or functions.php.

Just go to Dashboard > Appearance > Theme Editor > Theme Functions and add the following code to the end of the existing code and click Update File.

 

 

// Hide some products from unlogged users and a specific product category
 
add_filter( 'woocommerce_product_query_tax_query', 'exclude_products_fom_unlogged_users', 10, 2 );
 
function exclude_products_fom_unlogged_users( $tax_query, $query ) {
 
    // On frontend for unlogged users
 
    if( ! is_user_logged_in() ){
 
        $tax_query[] = array(
 
            'taxonomy'  => 'product_cat',
 
            'field'     => 'slug',
 
            'terms'     => array('t-shirts'), // <=== HERE the product category slug
 
            'operator'  => 'NOT IN'
 
        );
 
    }
 
    return $tax_query;
 
}

 

You can change Taxonomy to the product category you want to hide or change field to the product slug you want to hide.

I changed product_cat with accessories and here is the output:

When Logged In

When Logged Out

Leave a Reply