Hide specific WooCommerce products from logged out users

Do you want repeat and loyal customers for your e-store? Entice them with exclusive sales they can only see once they’ve created an account in your site.

Besides exclusive offers and give-aways with discount coupons, you can hide a few products, or an entire category of products from guests browsing your site and logged out users. We will see how in this tutorial.

Getting ready

Before doing any code modifications, it’s always a good idea to take a full backup of our site and make sure that we have FTP access or access to cPanel or Plesk or other control panel with access to a file manager.

If you wonder what is the best way to add code snippets to your site, have a look at our previous article about safely adding PHP code.

1- Allow sale of single product only with direct link

A great and frequently overlooked option, built-in in WooCommerce, is to hide a product entirely from your shop. Then you can send to selected customers or your email list the direct link to access and buy the specific product.

How? Very easy.

On the dashboard, select the product you want to hide from your shop and go to the “Edit” page. Press the “EDIT” button on “Catalog visibility”, as shown in the picture 1, and you will be presented with a list of options, as shown in the picture 2.
Screenshot - Edit Catalogue Visibility
Screenshot - set visibility to "hidden"

Select “Hidden” and press “OK”.

Now your product won’t be visible anywhere in your shop, and you can grant access to selected customers by sharing the link

2- Hide an entire product category from guests

This is useful if you want to hide more than a few products. Editing one by one individually, as well as sending a list of links to your customers is cumbersome. However, you can assign the same category to all the products you want to hide from public view and use the following snippet to make them only visible to logged-in users:
[codesyntax lang=”php”]

// Hide products in specific category from not logged-in users
add_filter( 'woocommerce_product_query_tax_query', 'exclude_products_fom_unlogged_users', 10, 2 );
function exclude_products_fom_unlogged_users( $tax_query, $query ) {
    if( ! is_user_logged_in() ){
        $tax_query[] = array(
            'taxonomy'  => 'product_cat',
            'field'     => 'slug',
            'terms'     => array('t-shirts'), // <=== EDIT HERE 'operator' => 'NOT IN'
        );
    }
    return $tax_query;
}

[/codesyntax]

Here is what this snippet does:

  • We hook our custom function to the woocommerce_product_query_tax_query filter, that appends taxonomy (ex., category, tag) queries to arrays
  • We create our custom function
  • We define the kind of taxonomy as category ( 'taxonomy' => 'product_cat' )
  • We decide that the categories will be called by slug
  • The specific category we want to hide is “t-shirts” ( 'terms' => array('t-shirts'), )
  • And this specific category should not be included in our query ( 'operator' => 'NOT IN' )

As we can see, this snippet gives us many possibilities, such as hide products based on tag (by changing 'taxonomy' => 'product_cat' to 'taxonomy' => 'product_tag' )

Result

We have hidden the category “T-shirts”, so the guest browsing your site will see:
Screenshot - guest viewing store

While, once they log in, they will also see all the t-shirts sold at your store
Screenshot - Registered customer viewing store

It’s worth to note that not logged-in users can still view the hidden products if they get a direct link to the product.

Wrapping up

Hiding some products from public view is advantageous as a marketing technique. It gives users that browse your site an incentive to create an account to your site, so that they can see more of the products you offer. You can also use this to motivate your customers to subscribe to your mailing list, and send them links to products hidden from public view.

Hiding a single product and making it only accessible by using the direct link is a built-in ability in WooCommerce. To hide an entire taxonomy (category or tag), is easily done by adding the code snippets presented in this article.

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

1 thought on “Hide specific WooCommerce products from logged out users”

  1. Great Snippet I was looking for. It would help many to have a clarification how to handle multiple categories to hide and if hiding a parent category will affect the child categories too.
    Also your missing line break before defining the operator might disable it actually as it stands within the remark.

    $tax_query[] = array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘slug’,
    ‘terms’ => array(‘category-slug-2’, ‘category-slug-2’), // ‘NOT IN’
    );

Leave a Comment

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