Hide product category or product on shop page

Hide product category or product on shop page

  • August 2, 2022

Hello Guys today we gonna learn how can we hide specific product category, specific product on shop page.

We are going to do this with the help of code snippets. You can directly add these code snippets in your child theme’s functions.php file or with the help of code snippet plugin.

1. Hide product category from shop page

add category slug with comma separated for hiding in shop page

/**
 * @snippet       hide category in shop page, archive page
 * @author        Deep Prakash Goyal
 * @website       https://wpexpertdeep.com/
 */


add_filter( 'get_terms', 'wpexpertdeep_get_subcategory_terms', 10, 3 );
function wpexpertdeep_get_subcategory_terms( $terms, $taxonomies, $args ) {
  $new_terms = array();
  // if it is a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
    foreach( $terms as $key => $term ) {
      //pass the slug name here
      if ( !in_array( $term->slug, array( 'hidden-product-cat', 'uncategorized' ) ) ) { 
      $new_terms[] = $term;
      }
    }
  $terms = $new_terms;
  }
  return $terms;
}

2. Hide specific category product in shop and archive page

add category slug with comma separated for hiding the products

/**
 * @snippet       hide specific category product in shop page, archive page
 * @author        Deep Prakash Goyal
 * @website       https://wpexpertdeep.com/
 */

add_action( 'woocommerce_product_query', 'wpexpertdeep_custom_pre_get_posts_query' );
function wpexpertdeep_custom_pre_get_posts_query( $q ) {
  $tax_query = (array) $q->get( 'tax_query' );
  $tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    // Don't display products in the clothing category on the shop page.
    'terms' =>array( 'hidden-product-cat', 'uncategorized'), 
    'operator' => 'NOT IN'
  );
  $q->set( 'tax_query', $tax_query );
}

With the help of these 2 methods you can hide category or product in shop page.

Add these snippets in your child theme’s functions.php file

Happy Coding !!!

Related Post

Change-in-stock-out-stock-text-in-Woocommerce-single-product-page

Change “in stock” / “out stock” text in Woocommerce single product page

In this tutorial you will learn how we can change “in stock” / “out stock” text with the custom. We will use custom snippet for this. Just copy the above snippet and paste in your […]

Change-product-price-in-cart-programmatically

Change product price in cart programmatically

Here we will learn, how we can change product price in cart programmatically. Add below code in your theme’s functions.php file

Fix-Images-and-Broken-Links-by-Updating-Paths-in-WordPress

Fix Images and Broken Links by Updating Paths in WordPress

Today we will learn how to fix broken images link issue in WordPress, after migrating site from local server to live server or (one domain to another). This is a very common issue. The simple […]