Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 418
Gman Avatar asked Dec 26 '13 00:12

Gman


People also ask

How do I get a specific category in WooCommerce?

Go to WooCommerce > Settings, select the Products tab, and then choose the Display option. For each of the Shop Page Display and Default Category Display options, select Show both. Click the Save changes button to save. Note: All the code mentioned below should be placed in the functions.

Can a product have multiple categories in WooCommerce?

Yes, a WooCommerce product can have multiple categories. This is because WooCommerce products are actually WordPress posts, and WordPress supports post categories.

How do I change the order of categories in WooCommerce?

Change Product Category Order in WooCommerce Simply visit Products » Taxonomy Order page to rearrange product categories. The plugin will list all your WooCommerce product categories. You can simply drag and drop to rearrange them in any order. Don't forget to click on the 'Update' button when you are finished.

How do I find the current product category name in WordPress?

If you want to get the current category ID, you can use the get_queried_object_id() function. This function will return the ID of the currently loaded object.


2 Answers

or maybe this:

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

foreach ($cat as $categoria) {
if($categoria->parent == 0){
   echo $categoria->name;
}
}
like image 117
Mauro Avatar answered Oct 31 '22 18:10

Mauro


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

}
like image 31
Gman Avatar answered Oct 31 '22 19:10

Gman