How to Hide a Product in WooCommerce or Hide Products by Category or Roles

There are many ways you can use to hide a specific product in WooCommerce and in this post; we will explore each of these ways to hide a product in WooCommerce.There are several reasons why you should consider to hide a product from the WooCommerce store but top on the list is when the product is out of stock or when the product is reserved for specific customers or user roles.

Hide WooCommerce Product

If you want to hide out of stock products, you should first consider adding the out of stock label on the product instead of hiding the product.

Nonetheless, if you still want to hide these out of stock products or any other WooCommerce products, I will share in this post how you can hide products by ID, name or by category.

Hide WooCommerce Product Using Options

WooCommerce by default comes with an excellent option that allows users to control the visibility of a product.

You can create a hidden product in WooCommerce by controlling the visibility option before you publish the image. To hide the product in WooCommerce using visibility, you need to follow the following steps:

  1. Log into your WordPress site dashboard
  2. Click on the products menu as shown on the image below so that you access the product you want to hide from users using a visibility option.How to Hide a Product in Woocommerce
  3. Select the product from the list of all products that you want to hide as shown below and click on the edit to access the product edit screen:How to Hide a Product in Woocommerce
  4. In the product editing screen on the right side under the publish metabox below there is an option named ‘Catalog visibility’ Click on this link and a drop-down with options will appear and the last option is Hidden.
    When you check this radio option and click on ok then update the post the product will be hidden from the shop catalog. As illustrated in these three steps (1,2,3) :How to Hide a Product in Woocommerce
  5. When you save these option and update the post, the product visibility will be set to hidden as you can see in the frontend of the shop:How to Hide a Product in Woocommerce
  6. If you check the catalog visibility option at the backend on the product edit screen you should see it was updated to hidden when the post was updated: How to Hide Product in Woocommerce

This is the quickest way to hide a WooCommerce product from the shop catalog. There are other ways you can use like adding code to the child theme. Let us explore more options you can use to hide product in WooCommerce.

Woocommerce Hide Specific Category Products from Shop Page

If you want to hide specific products from the shop page in WooCommerce you can achieve this using an action hook that targets the query of the shop page ‘woocommerce_product_query’.

The callback function should use the $tax_query to narrow down to the terms associated with the product you want to hide.

In this case, the terms key in the $tax_query array is equivalent to the category/tag as I explained in the post how to get current WooCommerce product category.

Suppose we want to hide this product from the ‘auto parts’ category.

hide woocommerce product

We can use the following code snippet added to the functions.php file to hide the product from the shop page:

/**
 * Hide WooCommerce product by category from shop page.
 *
 * @param $q
 */
 

add_action( 'woocommerce_product_query', 'njengah_hide_product_by_category_shop_page' ); 

function njengah_hide_product_by_category_shop_page( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'uncategorised','auto-parts'), // Don't display products in the 'auto parts' category on the shop page.
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}

Add this code to the theme functions and you can replace the ‘auto-parts’ category with any other category you wish to hide products and this will effectively hide the products from that category in the shop page as seen below :

hide woocommerce product

WooCommerce Hide Products Unless Logged In

Another common way to hide products in WooCommerce involves the restriction of the product visibility based on the status of the user whether logged or not logged.

The first step is obviously to check if the user is logged in. I explained in detail here – how to check if the user is logged in in WordPress.

We are going to use this code to check the login status then combine it with the query to hide WooCommerce products conditionally depending on the logged in status of the user.

The code should be as follows:

**
 * Hide WooCommerce product if user is not logged in 
 *
 * @param $query
 */
 

function njengah_hide_product_if_user_not_logged( $query ) {
	
	
    if ( $query->is_main_query() && is_woocommerce() && !is_user_logged_in() ) {
		
        $tax_query = array(
            array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => array( 'uncategorised','auto-parts'), // Don't display products in the 'auto parts' category on the shop page.
                    'operator'=> 'NOT IN' // exclude
                )
            );

        $query->set('tax_query', $tax_query);
    }
}

add_action( 'pre_get_posts', 'njengah_hide_product_if_user_not_logged' );

As you can see we are conditionally checking if the user is logged in using the is_user_logged_in() function before we can proceed with the query.

Woocommerce Hide Products by User Roles

You can also hide the products by the user roles. You need to first get the currently logged in user and their user role as I outlined in the previous tutorial on how to get the current user role in WordPress. This code to get user role is simply as follows:

        $user = wp_get_current_user(); // getting & setting the current user

        $roles = ( array ) $user->roles; // obtaining the role

        $roles[0] //current user role

We should combine this code with the previous pre_get_posts action and the code that will hide the product for all users expect the admin is as follows:

/**
 * Hide WooCommerce product by the user role like in this case only admin can see this product 
 *
 * @param $query
 */


function njengah_hide_product_by_user_role( $query ) {
	
	 $user = wp_get_current_user(); // getting & setting the current user 
	 $roles = ( array ) $user->roles; // obtaining the role 
	
	
    if ( $query->is_main_query() && is_woocommerce() &&  $roles[0] != 'administrator' ) {
		
        $tax_query = array(
            array(
                    'taxonomy' => 'product_cat',
                    'field' => 'slug',
                    'terms' => array( 'uncategorised','auto-parts'), // Don't display products in the 'auto parts' category on the shop page.
                    'operator'=> 'NOT IN' // exclude
                )
            );

        $query->set('tax_query', $tax_query);
    }
}

add_action( 'pre_get_posts', 'njengah_hide_product_by_user_role' );

As you can see in this line of code where we are conditionally checking the query I have introduced the condition to check if the user role is not admin – $roles[0] != ‘administrator’ then we proceed with the query. This is how you hide WooCommerce products based on the user role.

You can expound this example to fit your specific needs and you can also combine all these cases to create one conditional way to hide WooCommerce products.

Final Thoughts

In this post, we have highlighted the three common ways you can hide WooCommerce products that include hiding by user role, hiding products unless the user is logged in and hiding the WooCommerce products without either of these conditions. We also outlined the simplest way of hiding WooCommerce products using the catalog visibility option that is provided in the product edit screen. I hope you can now easily implement either of these solutions to hide WooCommerce products from your store.

Comments are closed.