woocommerce offtopic – echo product parent category

Question

im trying to echo the product sub category name with link.
but im getting the full categories ( parent + sub categories )

function add_category_above_product_title() {
global $product;
$product_category_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );
if ( ! empty( $product_category_ids ) ) {
    echo 'Category: ';
    foreach( $product_category_ids as $cat_id ) {
        $term = get_term_by( 'id', $cat_id, 'product_cat' );
        $link = get_term_link( $term, 'product_cat' );
        echo '<a href="'.esc_url($link).'">'.$term->name.'</a>&nbsp;&nbsp;';
    }
}

}
add_action( ‘woocommerce_single_product_summary’, ‘add_category_above_product_title’, 2 );

0
TIKAIR2000 3 weeks 2021-07-03T14:09:10-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse