stack
stack

Reputation: 103

Exclude categories from WordPress WooCommerce shop

I have been trying to find out a way to exclude a particular category called featured from my WooCommerce shop front page which displays by show subcatagories.

I found this script to place in my WordPress theme's functions.php file but nothing appears to happen:

add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( 'featured' ),
        'operator' => 'NOT IN'
    )));

    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

Upvotes: 0

Views: 3218

Answers (4)

Ali.Ghodrat
Ali.Ghodrat

Reputation: 3704

You have to add below codes to functions.php to your child theme

At the first you have to find categories slug and here how you can do this:

Remember a category SLUG is the category short title.

STEP 1.

/*get cat slug or cat info*/
     add_action('woocommerce_archive_description', 
'woocommerce_category_description', 2);
   function woocommerce_category_description() {
      if (is_product_category()) {
      global $wp_query;
    $cat = $wp_query->get_queried_object();
    echo "CAT IS:".print_r($cat,true); // the category needed.
}}

Then you can see categories information when you entering any categories on the shop page.

So you have to add this section which filters any specific category that you don't want to show up in the shop page:

STEP 2.

    /* Exclude Category from Shop*/
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
  $new_terms = array();

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
    foreach ( $terms as $key => $term ) {
      if ( ! in_array( $term->slug, array( 'myCat1', 'myCat2, 'myCat3','myCat4' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}

Remember to write your categories slug names instead of myCat1...myCat4

You have to enter categories slug like this: if ( ! in_array( $term->slug, array( 'myCat1', 'myCat2', 'myCat3','myCat4' )

Good luck :)

Upvotes: 0

James Hayes
James Hayes

Reputation: 111

I have simply commented these two lines and found to be working. I am not sure about its subsequent consequences on commenting these lines.

//if ( ! $q->is_main_query() ) return;
//if ( ! $q->is_post_type_archive() ) return;

Upvotes: 0

SCabralO
SCabralO

Reputation: 395

In my case I wanted to disable all the categories from my store's product list, including the future ones so I went to the archive-product.php file and commented the woocommerce_product_subcategories() function.

<?php woocommerce_product_loop_start(); ?>
   <?php //woocommerce_product_subcategories(); //This line of code ?>
   <?php while ( have_posts() ) : the_post(); ?>
      <?php wc_get_template_part( 'content', 'product' ); ?>
   <?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>

This way I won't have to come back every time my client creates a new category of products.

Upvotes: 0

stack
stack

Reputation: 103

To solve my above issue - I wanted to exclude a particular category from being posted anywhere on my shop page.

I added this to the page content-product_cat.php right at the top just beneath the opening <?php tag and amended the category name to reflect the category I needed to hide. You ad multiple categories by separating with commas.

if ( is_shop() && in_array( $category->slug, array( 'featured' ) ) ) {
  return;
}

Upvotes: 1

Related Questions