Get the product category name and description in Woocommerce Single Product page

GibsonFX picture GibsonFX · Jul 2, 2018 · Viewed 8.4k times · Source

I've been using the WooCommerce Codex, but I can't seem to get the data to display. I simply want to display the product category and descriptions to display on single product pages for my own custom layout like so.

<?php global $product; echo $product->get_attributes; ?>

<?php global $product; echo $product->get_short_description; ?>

Answer

LoicTheAztec picture LoicTheAztec · Jul 2, 2018

As you can have many product categories for a product, you will need to use a foreach loop. The $term variable is the WP_Term object…

<?php 
    foreach( wp_get_post_terms( get_the_id(), 'product_cat' ) as $term ){
        if( $term ){
            echo $term->name . '<br>'; // product category name
            if ($term->description)
                echo $term->description . '<br>'; // Product category description
        }
    }
?>

Tested and works