Edited (1)
You will need to use 'child_of'
from WP_Term_Query
available arguments, that will let you to get all child terms for the current product category parent term:
// Only on product category archive pages
if( is_product_category() ) {
$main_term = get_queried_object();
$args_query = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'child_of' => $main_term->parent
);
if ( $main_term->parent != 0 ) {
// Loop through WP_Term Objects
foreach ( get_terms( $args_query ) as $term ) {
if( $term->term_id != $main_term->term_id ) {
// $term->slug; // Slug
// Output each (linked) term name…
echo sprintf( '<a href="https://wordpress.stackexchange.com/questions/361045/%s">%s</a></br>', get_term_link( $term->term_id, 'product_cat' ), $term->name );
}
}
}
}
Or you can also use get_term_children()
like:
// Only on product category archive pages
if( is_product_category() ) {
$main_term = get_queried_object();
$taxonomy = 'product_cat';
if ( $main_term->parent != 0 ) {
$child_ids = get_term_children( $main_term->parent, $taxonomy );
echo '<ul>';
foreach ( $child_ids as $child_id ) {
if( $child_id != $main_term->term_id ) {
$term = get_term_by( 'id', $child_id, $taxonomy );
echo '<li><a href="' . get_term_link( $child_id, $taxonomy ) . '">' . $term->name . '</a></li>';
}
}
echo '</ul>';
}
}
This is only for product category archive pages, as for product pages you can have many product categories set for a product and the code will be complicated and quiet different…