<?php
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>';
}
}
if ($last_parent_cat_value == 8) {
echo '<h2>Bijbehorende bevestigingsmaterialen</h2>';
} elseif ($last_parent_cat_value == 9) {
echo '<h2>Bijbehorende apparaten</h2>';
} else {
echo '<h2>Bijbehorende producten</h2>';
}
?>
#woocommerce