As a first condition: Have you tried, if you retrieve sub categories at all?
$sub_categories = get_terms( 'product_cat', $args2 );
echo '<pre>';print_r( $sub_categories );
If you have sub categories, you would still have a problem with your foreach
:
foreach ($sub_categories as $subcat) {
$sub_categories = $sub_categories.$subcat->name.',';
}
You are overwriting $sub_categories
with the first time, you enter the loop. So, if you would have subcategories you would still have a problem here. Try something like
$text="";
$i = 0;
foreach ($sub_categories as $subcat){
if( $i > 0 )
$text .= ', ';
$text .= $subcat->name;
$i = 1;
}
echo $text;
So, taking your first code into account, this might help:
<?php
$parent_categories="" ;
$sub_categories="";
$subcat="";
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids,
'hierarchical'=> true,
'parent' => 0
);
$product_categories = get_terms( 'product_cat', $args );
foreach( $product_categories as $cat ) {
if($cat->slug == 'essays') {
$args2 = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids,
'parent' => $cat->term_id,
'hierarchical'=> true,
);
$sub_categories = get_terms( 'product_cat', $args2 );
$text="";
$i = 0;
if( is_array( $sub_categories ) && count( $sub_categories ) > 0 ){
foreach ($sub_categories as $subcat){
if( $i > 0 )
$text .= ', ';
$text .= $subcat->name;
$i = 1;
}
$sub_categories; = $text;
} else {
$sub_categories="nomatch";
}
}
}
?>