Reputation: 1
does anyone know how to hide one specific category tag name from the list in WooCommerce Product page?
Example: Product 1 belongs to Category A, B, C. I do not want to show category B in tag list on product page. Only A and C.
Thanks in advance for help.
Upvotes: 0
Views: 1370
Reputation: 1
Thank you helgatheviking. I coded it manually creating a custom function.
I paste it here, so it could be helpful in the future.
function hide_product_cat() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
if(!empty($terms)){
echo "<span class='prod_categories'>" . _n( 'Category:', 'Categories:', count($terms), 'woocommerce' ) . " ";
foreach ($terms as $term) {
$woo_cat_id = $term->term_id; //category ID
$woo_cat_name = $term->name; //category name
$woo_cat_slug = $term->slug; //category slug
if($woo_cat_id != <MY_CATEGORY_ID_B> ){
$result .= '<a href="' . get_term_link( $woo_cat_slug, 'product_cat' ) . '" rel="tag">' . $woo_cat_name . '</a>, ';
}
}
$result = substr($result, 0, -2) . ".";
echo $result;
echo "</span>";
}
}
Upvotes: 0
Reputation: 26319
Building on my tutorial on modifying the product query and using the appropriate WP_Query Parameters I think you could do something like the following to exclude all products in the product category with the slug "category-b". You will need to adjust the slug as needed. Untested.
add_action( 'woocommerce_product_query', 'so_31478197_product_query' );
function so_31478197_product_query( $q ){
$tax_query => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-b',
'operator' => 'NOT IN',
),
),
$q->set( 'tax_query', (array) $tax_query );
}
Upvotes: 1