Posted on

WooCommerce: Add Product Category to CSS Body Class

A Business Bloomer fan asked me an interesting question this week: how to apply CSS on the single product page based on the product category? Well, the answer is pretty simple: if we’re able to add the category name to the <body>, this can then be targeted in your custom CSS. So, let’s see how this is done!

WooCommerce: Add Product Category Name to Body CSS Classes

Thankfully, there was already some literature available on GitHub, and despite being 5 years old it still works like a charm. So, many thanksΒ Michael Krapf πŸ™‚

PHP Snippet: Add WooCommerce Product Category as Body CSS Class


/**
* @snippet Product Category > Body CSS Class | WooCommerce
* @how-to Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode https://businessbloomer.com/?p=21507
* @author Rodolfo Melogli
* @testedwith WooCommerce 2.6.12, WordPress 4.7.1
*/

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

Where to add this code?

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. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance πŸ™‚

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares πŸ™‚ Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

4 thoughts on “WooCommerce: Add Product Category to CSS Body Class

  1. Fine.
    My problem is to add a small CSS code to be used only in category page.
    The theme I use has a problem and don’t give to me this opportunity, so I wish to use functions.php

    This is my code:
    .shop-page-title { height:300px !important; max-height:300px !important; }

    1. Hey Giancarlo, you can definitely target the “archive” pages via CSS only πŸ™‚

  2. Hi,
    Thank you so much for your helpful blogs and videos. I wish to change the colour & logo image in my menu navigation on my product category page and single products in that category. How can I edit this to include the product category page also?

    Thank you πŸ™‚

    1. Hey Rachael thanks so much for your comment! Once the category is in the class, you can do whatever you like with CSS on that specific page – as you said this only works on the single product page. In fact, the category page already has a body class with the name of the category – so there is no need for us to add it; if you target that in your CSS you’ll be able to achieve what you like. Let me know!

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

Your email address will not be published. Required fields are marked *