StackOverflow community. I would like to ask for your help.
My problem is that I want to display all subcategories text in the main category card-box. Using this code,
add_action('woocommerce_after_subcategory_title', 'woocommerce_subcats_from_parentcat_by_ID', 30);
function woocommerce_subcats_from_parentcat_by_ID($category)
{
$parent_category_ID = $category->term_id;
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0, // Set to 0 to show empty categories and 1 to hide them
'parent' => $parent_category_ID,
'taxonomy' => 'product_cat'
);
$subcategories = get_categories($args);
echo '<div class="custom_subcategory_main">';
echo '<span class="custom_subcategory_klasy">Dostępne klasy:</span><br>';
foreach ($subcategories as $subcategory) {
echo '<span> '.$subcategory->name.'</span>';
}
echo '<br>';
echo '<span class="custom_subcategory_klasy">Poziom:</span><br>';
echo '<span>Szkoła podstawowa</span>';
echo '</div>';
}
the subcategories text is displaying correctly in the main category card-box [screen below].
The problem is that after navigating to the product subcategory it is still visible and I would like to hide it. More precisely, I would like to hide this part of the code [screen below].
echo '<span class="custom_subcategory_klasy">Dostępne klasy:</span><br>';
CodePudding user response:
If I understand properly, once you're in a subcategory, there are no further children. So you could put a check inside your function to see if children exists, and if not, exit:
add_action('woocommerce_after_subcategory_title', 'woocommerce_subcats_from_parentcat_by_ID', 30);
function woocommerce_subcats_from_parentcat_by_ID($category)
{
$parent_category_ID = $category->term_id;
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0, // Set to 0 to show empty categories and 1 to hide them
'parent' => $parent_category_ID,
'taxonomy' => 'product_cat'
);
$subcategories = get_categories( $args );
if ( empty ( $subcategories ) ) return; // exit
echo '<div class="custom_subcategory_main">';
echo '<span class="custom_subcategory_klasy">Dostępne klasy:</span><br>';
foreach ($subcategories as $subcategory) {
echo '<span> '.$subcategory->name.'</span>';
}
echo '<br>';
echo '<span class="custom_subcategory_klasy">Poziom:</span><br>';
echo '<span>Szkoła podstawowa</span>';
echo '</div>';
}