<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">add_filter( 'body_class', 'wc_product_term_css_body_class' );
function wc_product_term_css_body_class( $classes ){
if ( is_singular( 'product' ) ) {
$current_product = wc_get_product();
$custom_terms = get_the_terms( $current_product->get_id(), 'product_cat' );
if ( $custom_terms ) {
foreach ( $custom_terms as $custom_term ) {
$classes[] = 'product_cat_' . $custom_term->slug;
}
}
}
return $classes;
}</pre>
Add wooCommerce product category as body CSS class on single product
Tags