3

I wanted to hide a certain product category in the category list on the Woocommerce shop page. I found and used the following snippet to do so:

add_filter( 'get_terms', 'exclude_category', 10, 3 );
function exclude_category( $terms, $taxonomies, $args ) {
  $new_terms = array();
  // if a product category and on a page
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) 
  {
    foreach ( $terms as $key => $term ) {
      if ( ! in_array( $term->slug, array( 'books' ) ) ) {
        $new_terms[] = $term;
      }
    }
    $terms = $new_terms;
  }
  return $terms;
}

I have a debug option in my wp-config set to true and so while the snippet is working and the 'books' category is hidden from the list, I am getting the following error:

Notice:: Trying to get property of non-object in

And it is pointing to this line:

if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() )

Is this line written correctly? or am I missing something?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Joe Bloggs
  • 1,410
  • 2
  • 24
  • 53

1 Answers1

10

Updated: You should better use unset() to remove the product category term from the array, this way:

add_filter( 'get_terms', 'exclude_category', 10, 3 );
function exclude_category( $terms, $taxonomies, $args ) {
    $new_terms = array();
    if ( is_shop() ){
        foreach ( $terms as $key => $term ) {
            if( is_object ( $term ) ) {
                if ( 'books' == $term->slug && $term->taxonomy = 'product_cat' ) {
                    unset($terms[$key]);
                }
            }
        }
    }
    return $terms;
}

This code goes on function.php file of your active child theme (or theme).

Tested and works (does't throws an error).

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your help .. it works only for the widget, but not for the categories list. Basically on my shop page, I've got a list of categories just below the shop title. It's not a widget though. The code that I have works for this one and the unwanted category disappears from the list, but it triggers the notice error as mentioned above .. – Joe Bloggs Feb 18 '18 at 09:20
  • What if I edit the archive-product.php template? The list of categories displays with `$terms = get_terms('product_cat');` Would I be able to exclude the category here instead? – Joe Bloggs Feb 18 '18 at 09:28
  • @JoeBloggs I have update my code… try it. It should not make any error this time. – LoicTheAztec Feb 18 '18 at 10:37
  • Thanks, the code works, but still i'm getting this stubborn `Notice :Trying to get property of non-object in` pointing to this line `if ( 'books' == $term->slug && $term->taxonomy = 'product_cat' )` Could it be something else? Conflicting with the code? – Joe Bloggs Feb 18 '18 at 22:12
  • @JoeBloggs I don't get that error… but I have lightly updated the code, using the PHP conditional `is_object()` on the `$term` to be sure that is an object (a WP_Term object instance)… So try it… I hope that it will work. – LoicTheAztec Feb 18 '18 at 22:26
  • you're a legend! That actually works now, no errors! Many thanks for you help on this. – Joe Bloggs Feb 19 '18 at 06:39