I want to add a custom class to the categories on the product archive page so that I can add custom styles to the categories tags.
Example:
Product X in Category 1 --> Category Tag in Yellow
Product Y in Category 2 --> Category Tag in Red
I'm using this php snippet, but this is for single product pages and is applied to the <body> element. How can I change it to work for shop archive pages as well?
add_filter( 'body_class','my_body_classes2' );
function my_body_classes2( $classes ) {
if ( is_product() ) {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
$classes[] = 'product-in-cat-' . $product_cat_id;
}
}
return $classes;
}