Advertisement
CodeDropz

WooCommerce - Add Body Class

Nov 7th, 2023 (edited)
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.46 KB | None | 0 0
  1. add_filter( 'body_class', 'custom_wc_product_cats_css_body_class' );
  2.  
  3. function custom_wc_product_cats_css_body_class( $classes ){
  4.   if ( is_singular( 'product' ) ) {
  5.     $current_product = wc_get_product();
  6.     $custom_terms = get_the_terms( $current_product->get_id(), 'product_cat' );
  7.     if ( $custom_terms ) {
  8.       foreach ( $custom_terms as $custom_term ) {
  9.         $classes[] = 'product_cat-' . $custom_term->slug;
  10.       }
  11.     }
  12.   }
  13.   return $classes;
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement