MrLoco
MrLoco

Reputation: 3

Hide certain product category in wordpress/woosommerce

I have tried to hide a certain subcategory from the category widget on shop page and other pages, there is a category widget. I found that the code below works for all pages except the shop page. If I change is_product_category() for the is_shop(), so it works for the shop page, but not for the other pages.

How can I do that it works for ALL pages (shop and all others)?

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_product_category() ) {

    foreach ( $terms as $key => $term ) {

      if ( ! in_array( $term->slug, array( 'seinakellad','nastennye-chasy','wall-clock' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}

Upvotes: 0

Views: 282

Answers (1)

Mr. Jo
Mr. Jo

Reputation: 5281

Just remove the additional check in your if condition:

add_filter( 'get_terms', 'filter_get_terms', 10, 3 );
function filter_get_terms( $terms, $taxonomies, $args ) {
    $new_terms = [];

    // if a product category and on the shop page
    if ( ! is_admin() ) {
        foreach ( $terms as $term ) {
            if ( ! in_array( $term->slug, [ 'seinakellad', 'nastennye-chasy', 'wall-clock' ] ) ) {
                $new_terms[] = $term;
            }
        }

        $terms = $new_terms;
    }

    return $terms;
}

Upvotes: 1

Related Questions