WooCommerce: 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 CSS 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 CSS Class @ Single Product
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.9
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
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;
}
You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"
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.3.
If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)
Need Help with WooCommerce?
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
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.
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!
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?
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!
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
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; }
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?
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 are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)
With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.
Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.
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!
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!
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?
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!
Still working, thank you for the help.
Cheers!
Nice one! I would be interested how that that works with woocommerce product tags instead of categories. Any hints are welcome ๐
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
Works like a charm ๐ Thanks!
Nice ๐
Absolutely fantastic! Works beautifully ๐
Awesome ๐
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; }
Hey Giancarlo, you can definitely target the “archive” pages via CSS only ๐
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 ๐
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!