I’ve found online a snippet of code that currently lists all categories on this WooCommerce website.
How to I make it specific to show the category related to the product they are viewing?
Here is the code that I’ve tweaked:
<div id="ListCat">
<h3>Listed in the following categories<?php the_category(); ?></h3>
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 0; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo ' <a href="'. get_term_link($cat->slug, 'product_cat')
.'">'. $cat->name .'</a>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo '<br><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
}
}
}
}
?>
Advertisement
Answer
There is a more much simpler way:
To display it as coma separated string (with a product category links for each):
// Display a coma separated string of the product categories for this product echo wc_get_product_category_list( get_the_id() );
(or completely similar):
// Display a coma separated string of the product categories for this product echo get_the_term_list( get_the_id(), 'product_cat' );
To display it as a formatted html list (with a product category links for each):
// Display a html formatted list of the product categories for this product echo '<ul>' . wc_get_product_category_list( get_the_id(), '</li><li>', '<li>', '</li>' ) . '</ul>';
(or completely similar):
// Display a html formatted list of the product categories for this product echo '<ul>' . get_the_term_list( get_the_id(), 'product_cat', '<li>', '</li><li>', '</li>' ) . '</ul>';
Explanations:
In Woocommerce 3 there is wc_get_product_category_list() dedicated function to list product categories from a product ID with some arguments available on it, like you can see in its source code:
/**
* Returns the product categories in a list.
*
* @param int $product_id
* @param string $sep (default: ', ').
* @param string $before (default: '').
* @param string $after (default: '').
* @return string
*/
function wc_get_product_category_list( $product_id, $sep = ', ', $before = '', $after = '' ) {
return get_the_term_list( $product_id, 'product_cat', $before, $sep, $after );
}
As you can see in this source code, it’s an alias of WordPress
get_the_term_list()function with'product_cat'as$taxonomyargument that you cans also use instead.