2
votes

On WooCommerce single product pages, under the add to cart button displays the product category list.

In my website I have 2 product categories named "In Ground Hoops", so it is getting displayed twice because it is a main category and a sub-category.

How can I hide this sub-category from being displayed there?

I can't target it with CSS and the woocommerce_template_single_meta hook is all or nothing from what I can find.

Any ideas on where to start and/or how to do it would be appreciated.

1

1 Answers

1
votes

Your product category and sub-category "In Ground Hoops" has the same term name but different term slugs for each:

  • Product category "In Ground Hoops" term slug is: 'in-ground-hoops'
  • Product sub-category "In Ground Hoops" term slug is: 'in-ground-hoops-goalrilla'

So the only way to distinguish both of them in the code is by their term ID or by their term slug. So I will use here the term Slug

When looking at the responsible template that is displaying the meta output, the only way to filter that specific product subcategory is using the available WP get_the_terms filter hook:

add_filter( 'get_the_terms', 'hide_specific_product_subcategory', 20, 3 );
function hide_specific_product_subcategory( $terms, $post_id, $taxonomy )
{
    // Only on single product pages and for product category custom taxonomy
    if( ! is_product() && $taxonomy != 'product_cat' ) return $terms; // We Exit

    $category = 'in-ground-hoops'; // your main product category
    $subcategory = 'in-ground-hoops-goalrilla'; // the related subcategory
    $taxonomy = 'product_cat'; // product category taxonomy

    if( has_term( $category, $taxonomy, $post_id ) ){
        foreach ($terms as $key => $term){
            if( $term->slug == $subcategory ){
                unset( $terms[$key] );
            }
        }
    }
    return $terms;
}

Code goes in function.php file of your active child theme (or active theme).

Tested and works.