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

or maybe this:

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

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

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

}

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);