I need to get the TOP level category (not just the parent category) of a subcategory of products in Woocommerce.
I have this code to get the parent category id:
if (is_subcategory()) {
$term = get_queried_object();
$parent_id = $term->parent;
}
And this one makes $parent_id as a body class:
add_filter( 'body_class', 'parent_id_body_class' );
function parent_id_body_class( $classes ) {
// add comprehensive text followed by parent id number to the $classes array
$term = get_queried_object();
$parent_id = $term->parent;
$classes[] = 'parent-id-' . $parent_id;
// return the $classes array
return $classes;
}
All this works fine, but this is NOT the top level parent category. It's just the parent. I've got 3 levels of categories. I'm not very skilled in php yet... I've searched a lot but couldn't find how figure this out. Your help would be very appreciated. Thank You.