WooCommerce - get category for product page

For my WC product pages, I need to add a class to the body tag so that I can perform some custom styling. Here's the function I'm creating for this...

function my_add_woo_cat_class($classes) {

    $wooCatIdForThisProduct = "?????"; //help!

    // add 'class-name' to the $classes array
    $classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;
    // return the $classes array
    return $classes;

//If we're showing a WC product page
if (is_product()) {
    // Add specific CSS class by filter

...but how do I get the WooCommerce cat ID?


$product->get_categories() is deprecated since version 3.0! Use wc_get_product_category_list instead.


A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.

global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
    $product_cat_id = $term->term_id;

Please look into the meta.php file in the "templates/single-product/" folder of the WooCommerce plugin.

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>

I literally striped out this line of code from content-single-popup.php located in woocommerce folder in my theme directory.

global $product; 
echo $product->get_categories( ', ', ' ' . _n( ' ', '  ', $cat_count, 'woocommerce' ) . ' ', ' ' );

Since my theme that I am working on has integrated woocommerce in it, this was my solution.

Similar questions

Retrieve Product category 'NAME' by product category ID - WooCommerce?
Can I retrieve woocommerce product category name by ID? Not thumbnail, not the URL, not the Sub-categories, just raw Category name.
Replace woocommerce product category thumbnail with image of latest product in respective category
I'm trying to update the category list/loop in woocommerce to replace the category thumbnail with a thumbnail of the newest available product in the respective category. I think I'm halfway to achieve this on a test page though I can list the latest product of each category and display the category name beneath the image though I can't get it to li...
Woocommerce / WP All Import: automatically add product to category if product name contains the category name
I want to automatically add products to a category if their name contains the category (for example the product name is: Awesome brand baseball bat, the product should automatically be added to the category baseball bat). Is there a plugin that can automatically do this or even better: is it possible to add a rule to WP All Import to do this? Setti...
add category name to permalinks on product page /category-name/product-name
I'm trying to reconfigure the permalinks used in WooCommerce such that the following format will be displayed: homesite/category/product. Right now, the permalinks only shows under a category page: homesite/?product_cat="category-name" Under the product page: homesite/?product="product-name" Whatever I tried under the Permalinks section of WP didn'...
How can I add below of Woocommerce out of stock product text the Woocommerce product price on a single product
I have a single woocommerce product page where only availability "Out of Stock" message is shown. How can I show a formatted price with currency below that text? I think I need to insert the following but I dont know how This is happening to all simple products. As settings, stock management is disabled. I don't know why price is not displayed. I t...
Get child product categories from parent product category in WooCommerce
In WooCommerce, I am trying to get all child product category terms from the parent term id of the current product category with the code below: The variable $category_parent_id returns the parent product category id and $category_test returns all product category terms I'm trying to return all child product categories using the parent product cate...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.