custom taxonomy – Hide empty categories on widget

Question

I excluded some products from woocommerce_product_query with this code:

  function en_archive_products_by_rol( $query ) {
    if (is_admin()) { 
          return;
        }
    
    $meta_query = (array)$query->get('meta_query');
    $meta_query = array_merge( $meta_query,wpf_do_hide_product());
    $query->set( 'meta_query', array_unique($meta_query) );
    return $query;
    }
}
add_action( 'woocommerce_product_query', 'en_archive_products_by_rol', PHP_INT_MAX  );

It works fine, but I’m trying to hide empty product categories on filter widget and it keeps counting excluded products in each category. I’m using this code:


function exclude_empty_terms_widget( $terms, $taxonomies, $args, $term_query ) {
    $new_terms = array();
    if ( ! empty( $terms ) && in_array( 'product_cat', $taxonomies ) ) {
        foreach ( $terms as $key => $term ) {
            if ( $term->count > 0 ) {
                $new_terms[] = $term;
            }
        }
        $terms = $new_terms;
    }
    return $terms;
}
add_filter( 'get_terms', 'exclude_empty_terms_widget', PHP_INT_MAX, 4 );

Any ideas of what is wrong?

0
Elianalu 2 months 2023-02-09T11:37:21-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse