You can try this:
function woo_prod_categories() {
if ( is_product() ) {
global $post;
$product_cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $product_cats ) && ! is_wp_error( $product_cats ) ) {
$cat_links = array();
foreach ( $product_cats as $product_cat ) {
$cat_links[] = '<a href="' . get_term_link( $product_cat->term_id, 'product_cat' ) .
'">' . $product_cat->name . '</a>';
}
return implode( ', ', $cat_links );
}
}
return '';
}
add_shortcode( 'woo_prod_categories', 'woo_prod_categories' );
The function checks if the current page is a product page and then retrieves the product categories (using get_the_terms()
function). It then generates a linked list of these categories which is then returned by the shortcode. The categories are separated by commas.