Gman
Gman

Reputation: 783

woocommerce - How do I get the most top level category of the current product category

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

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

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

I already tried doing 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 It didn't worked at all and it brakes the page from loading after woocomerce_get_term... I'm not sure what to do at this point It

thanks for any help on this.

Upvotes: 17

Views: 55398

Answers (7)

Joseph Adegbola
Joseph Adegbola

Reputation: 59

Use this to get the Top Parent Category:

$uncategorized_term_id = get_option( 'default_product_cat' );

Upvotes: -2

Nathan
Nathan

Reputation: 398

For those looking for a clean solution, I've extended @Nicolas GRILLET's function so that you only need to pass in a product ID and you get the term ID, Name and Link (URL).

function get_parent_terms(int $product_id)
{
    // Get all the terms associated with the product
    $product_terms = get_the_terms($product_id, 'product_cat');

    // Check if there are terms associated with the product
    if ($product_terms) {

        // Loop through $product_terms to find parent terms
        foreach ($product_terms as $product_term) {

            // Climb up the taxonomy hierarchy until parent found
            while ($product_term->parent > 0) {
                $product_term = get_term_by("id", $product_term->parent, "product_cat");
            }

            // Store parent terms as objects in array $parent_terms
            $parent_terms[] = (object)[
                'term_id' => $product_term->term_id,
                'name' => $product_term->name,
                'link' => get_term_link($product_term->term_id, 'product_cat')
            ];
        }

        // Return array of parent terms
        return $parent_terms;
    } else {
        // Return false if there are no terms 
        return false;
    }
}

This will give you an array of all the top-level categories a product belongs to.

Assuming your products only have one top-level category then the example below shows how to get the product's top-level category id, name and link.

// Get the first top level category (object)
$top_level_cat = get_parent_terms($product_id)[0];

// Echo the ID
echo $top_level_cat->term_id;

// Echo the Name
echo $top_level_cat->name;

// Echo the Link/URL
echo $top_level_cat->link;

Upvotes: 1

Nicolas GRILLET
Nicolas GRILLET

Reputation: 111

Here the solution 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);

Upvotes: 10

Siddhartha Chowdhury
Siddhartha Chowdhury

Reputation: 2732

Here if u have the a "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's 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

Upvotes: 0

thorne51
thorne51

Reputation: 618

I know this is an old post, but I had a similar situation where we needed to get the root category of the current product being viewed. The simplest solution I could think of was to look at how WooCommerce does its breadcrumbs, and this piece of code is what did the trick for 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
    }
}

Upvotes: 10

Mauro
Mauro

Reputation: 1481

or maybe this:

$cat = get_the_terms( $product->ID, 'product_cat' );

foreach ($cat as $categoria) {
if($categoria->parent == 0){
   echo $categoria->name;
}
}

Upvotes: 32

Gman
Gman

Reputation: 783

After a lot of research I figured a way to solve this. I hope this will help someone.

solution:

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>';
    }

}

Upvotes: 27

Related Questions