Add Custom CSS to Woocommerce Product Page in a specified category

Here’s my solution… Added the following to functions.php in my child theme:

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;
}

Now whenever you’re viewing a single product page, an additional CSS class for each category the product is in will be added to the <body> tag.

Then if you want to change the styling for any single product pages in category 2 or 4, you can use the following CSS:

.product-in-cat-2 , .product-in-cat-4 {
    color: #ffffff;
}