I'm trying to get the parent category of a product when actually it's assigned to a child category of the parent, but not the parent itself. The function below works when I assign both the child and parent category for the product. But I want only to assign the product to the child category and in the same time to display the parent category too. Like that in this order: ChildCatName - ParentCatName Could you please point me in the right direction? Thank you!
//Woo Cat product
$woo_single_cats = get_the_terms( $post->ID, 'product_cat' );
if ( $woo_single_cats && ! is_wp_error( $woo_single_cats ) ) {
$woo_single_cat = array();
foreach ( $woo_single_cats as $term ) {
$woo_single_cat[$term->term_id] = $term->name;
}
$woo_single_cat = apply_filters('seopress_titles_product_cat', $woo_single_cat);
$woo_single_cat_html = stripslashes_deep(wp_filter_nohtml_kses(join( " - ", $woo_single_cat )));
}