Woocommerce - How to get the top level category of the current product category

I have a Woocommerce product and I need to display the top level category of the category assigned to the product on this page

- Main Product Category -- Sub Product Category --- Category Assigned to product 

I need to get the identifier or name "Main Product Category" so that I can display it in the category of one product.

I have already tried the following:

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

But that didn’t work at all, and it slows down the page after loading after woocomerce_get_term ... I'm not sure what to do at this moment. It

Thanks for any help on this.

+10
php wordpress woocommerce
source share
6 answers

After many studies, I decided a way to solve this problem. Hope this helps someone.

decision:

 global $post; $prod_terms = get_the_terms( $post->ID, 'product_cat' ); foreach ($prod_terms as $prod_term) { // gets product cat id $product_cat_id = $prod_term->term_id; // gets an array of all parent category levels $product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' ); // This cuts the array and extracts the last set in the array $last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true); foreach($last_parent_cat as $last_parent_cat_value){ // $last_parent_cat_value is the id of the most top level category, can be use whichever one like echo '<strong>' . $last_parent_cat_value . '</strong>'; } } 
+23
source share

or maybe this:

 $cat = get_the_terms( $product->ID, 'product_cat' ); foreach ($cat as $categoria) { if($categoria->parent == 0){ echo $categoria->name; } } 
+21
source share

I know this is an old post, but I had a similar situation when we needed to get the root category of the current product. The simplest solution I thought of is to watch how WooCommerce makes its breadcrumbs, and this piece of code is what helped me:

 if ( is_product() ) { global $post; $terms = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) ); if ( ! empty( $terms ) ) { $main_term = $terms[0]; $ancestors = get_ancestors( $main_term->term_id, 'product_cat' ); if ( ! empty( $ancestors ) ) { $ancestors = array_reverse( $ancestors ); // first element in $ancestors has the root category ID // get root category object $root_cat = get_term( $ancestors[0], 'product_cat' ); } else { // root category would be $main_term if no ancestors exist } } else { // no category assigned to the product } } 
+6
source share

Here is the solution that I actually use

 function get_parent_terms($term) { if ($term->parent > 0){ $term = get_term_by("id", $term->parent, "product_cat"); return get_parent_terms($term); }else{ return $term->term_id; } } global $wp_query; $cat_obj = $wp_query->get_queried_object(); $Root_Cat_ID = get_parent_terms($cat_obj); 
+5
source share

Here if u has "category_ID"

 /*If you dont have a category ID use this: * $category_ID = get_queried_object()->term_id; * This will get you the current category_ID */ $termid = get_term($category_ID, 'product_cat' ); if($termid->parent > 0) { // get the parent hierarchy. $cat_hierachy = get_ancestors( $category_ID, 'product_cat' ); return end($cat_hierachy); // returns the Level-1 category_ID } return $category_ID; // Has no parent so return THIS category_ID Level-1 
0
source share

This is a feature that I developed to use.

 /** * Get product top category * @param int $pid. The product ID * * @return int. Returns project parent ID */ function get_product_top_category($pid) { global $wpdb; $cat_id = $wpdb->get_var('SELECT b.term_id FROM '.$wpdb->prefix.'term_relationships a, '.$wpdb->prefix.'term_taxonomy b WHERE a.object_id = "'.$pid.'" AND a.term_taxonomy_id = b.term_taxonomy_id AND b.parent = "0" AND b.taxonomy = "product_cat" LIMIT 1'); return $cat_id; } 

So you can get the top category of the current product using this

 $top_level_term = get_product_top_category($post->ID); 

A limitation of this code is that it can return multiple top-level categories of any product. To combat this, I used LIMIT 1 to return only one category that suits me.

0
source share

All Articles