the situation is that when on a product category which is parent category and has children I should list the children categories of that parent, that is the first part of the code and works as it should then the "elseif" part is the issue. I need to display child categories of a parent category of that child when on the child category. That also works, but I cannot seem to find out solution that when a person is on a category which itself is a parent (without child categories) that it shows all top level (main) categories. Actually, that also does work with the "else" part of the code, but I don't know which condition to put to the "elseif" block, so that is recognizes that..
Point is: "elseif" condition part I cannot solve so that if on a product category which is parent and there is no children that the conditions returns false so it can go to "else" block and execute code.
Thank you for time time :)
<?php
$queried_object = get_queried_object();
$parent = $queried_object->term_id;
$parent_categories = get_terms( 'product_cat', array( 'child_of' => $parent ) );
$child_terms = get_term_children ( $queried_object->term_id, 'product_cat' );
$based_term = (is_wp_error($child_terms) || empty($child_terms)) ? get_term ( $queried_object->parent, 'product_cat' ) : $queried_object;
if ( $parent_categories && ! is_wp_error( $parent_categories ) ) {
echo '<ul class="product-category-listing" >';
$term = get_term( $parent_categories, 'product_cat' );
foreach($parent_categories as $parent_category) :
$term = get_term( $parent_category, 'product_cat' );
echo '<li class="product-category-title" >';
echo '<a class="product-category-title-btn" href="'.get_term_link($term).'" >';
echo $term->name;
echo '</a>';
echo '</li>';
endforeach;
} elseif ( $parent_categories !== false ) {
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => $based_term->term_id,
'orderby' => 'title',
'order' => 'ASC',
);
$product_cat = get_terms( $args );
foreach ($product_cat as $parent_product_cat)
{
echo '<ul class="product-category-listing '.$parent_product_cat->slug.'">
<li class="product-category-title">
<a href="'.get_term_link($parent_product_cat->term_id).'" class="product-category-title-btn">'.$parent_product_cat->name.'
<svg width="8" height="12" viewBox="0 0 8 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7 6.53125C7.28125 6.25 7.28125 5.78125 7 5.46875L2.75 1.21875C2.4375 0.9375 1.96875 0.9375 1.6875 1.21875L0.96875 1.9375C0.6875 2.25 0.6875 2.71875 0.96875 3L4 6.03125L0.96875 9.03125C0.6875 9.3125 0.6875 9.78125 0.96875 10.0938L1.6875 10.7812C1.96875 11.0938 2.4375 11.0938 2.75 10.7812L7 6.53125Z" fill="#1ABFDF"/>
</svg>
</a>
<ul class="product-category-single">';
$child_args = array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => $parent_product_cat->term_id,
'orderby' => 'title',
'order' => 'ASC',
);
$child_product_cats = get_terms( $child_args );
foreach ($child_product_cats as $child_product_cat)
{
echo '<li><a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
}
echo '</ul>
</li>
</ul>';
}
}
else {
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => 0,
'orderby' => 'title',
'order' => 'ASC',
);
$product_cat = get_terms( $args );
foreach ($product_cat as $parent_product_cat)
{
echo '<ul class="product-category-listing '.$parent_product_cat->slug.'">
<li class="product-category-title">
<a href="'.get_term_link($parent_product_cat->term_id).'" class="product-category-title-btn">'.$parent_product_cat->name.'
<svg width="8" height="12" viewBox="0 0 8 12" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7 6.53125C7.28125 6.25 7.28125 5.78125 7 5.46875L2.75 1.21875C2.4375 0.9375 1.96875 0.9375 1.6875 1.21875L0.96875 1.9375C0.6875 2.25 0.6875 2.71875 0.96875 3L4 6.03125L0.96875 9.03125C0.6875 9.3125 0.6875 9.78125 0.96875 10.0938L1.6875 10.7812C1.96875 11.0938 2.4375 11.0938 2.75 10.7812L7 6.53125Z" fill="#1ABFDF"/>
</svg>
</a>
<ul class="product-category-single">';
$child_args = array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => $parent_product_cat->term_id,
'orderby' => 'title',
'order' => 'ASC',
);
$child_product_cats = get_terms( $child_args );
foreach ($child_product_cats as $child_product_cat)
{
echo '<li><a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
}
echo '</ul>
</li>
</ul>';
}
};
?>```