0
votes

Can somebody come up with something clever on how to add a custom body class or a class to a specific wrapper div from the woocommerce product category page?

I have thought of using the category description, but it dosent look like a nice solution. I want it just to generate two classes, either "light" or "dark".

Anyone??

Kind regards, Ricahrd

2

2 Answers

2
votes

Are you echo'ing body classes? If so, have you tried hooking in to the body_class-function in Wordpress? (https://codex.wordpress.org/Plugin_API/Filter_Reference/body_class)

something like:

add_filter('body_class', 'add_custom_body_class');

function add_custom_body_class($classes){
   if(is_product_category()){
     $classes[] = 'light';
   }
   return $classes;
}
0
votes

PHP Snippet: Add WooCommerce Product Category as Body CSS Class
You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file.

add_filter( 'body_class', 'bbloomer_wc_product_cats_css_body_class' );

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