WooCommerce: Product Category as Body Class @ Single Product Page

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 HTML “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

PHP Snippet: Add WooCommerce Product Category as Body Class @ Single Product Page

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 ๐Ÿ™‚

/**
 * @snippet       Product Category > Body Class @ Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */
 
add_filter( 'body_class', 'bbloomer_wc_product_cats_css_body_class' );
 
function bbloomer_wc_product_cats_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;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

16 thoughts on “WooCommerce: Product Category as Body Class @ Single Product Page

  1. Hey, I have been following your tutorial for a long time. But I could not find any related to “always show selected WooCommerce category on top”

    FOR example, I have a few categories:

    Fan
    Fan 1
    Fan 2
    Shoes
    Shoes 1
    Shoes 2
    Socks

    When I clicked the SOCKS category, it should look like as below :

    Socks
    Socks 1
    Socks 2
    Fan
    Shoes

    When I clicked the SHOES category, it should look like as below :

    Shoes
    Shoes 1
    Shoes 2
    Fan
    Socks

    CSS would be much appreciate, or if there is any extra plugin I can use also will be appreciate. Thanks!

    1. Hi Nani, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Any advice on how to do this in the cart and checkout pages, so that it adds classes of the product category based on what items are in the cart/checkout?

    1. Hi Jon, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Still working, thank you for the help.

    1. Cheers!

  4. Nice one! I would be interested how that that works with woocommerce product tags instead of categories. Any hints are welcome ๐Ÿ™‚

    1. Rainer, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  5. Works like a charm ๐Ÿ˜‰ Thanks!

    1. Nice ๐Ÿ™‚

  6. Absolutely fantastic! Works beautifully ๐Ÿ™‚

    1. Awesome ๐Ÿ™‚

  7. 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 ๐Ÿ™‚

  8. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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