Add product category to body classes of Woocommerce single product page

To customize the style of single product page for some particular category of products on WooCommerce you can use the following code in your functions.php file.

function woo_custom_taxonomy_in_body_class($classes){
if( is_singular('product')) {
$custom_terms = get_the_terms(0, 'product_cat');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
$classes[] = 'product_cat_' . $custom_term->slug;
}
}
}
return $classes;
}
add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );

If you want to add the tags just replace product_cat with product_tag

Tagged: