0

I have searched this forum for the possible solutions but I am not getting anything which works. Maybe I am doing it the wrong way.

Here is the code that I have tried so far

$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) {
//var_dump($cat); echo '<br>';

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 );

    foreach ($sub_categories as $subcat) {
        $sub_categories = $sub_categories.$subcat->name.',';
    }

    $sub_categories = rtrim($sub_categories);
}
else
    $sub_categories= 'nomatch';

}

When i do echo $sub_categories.

I get the output no match;

Debug results

I tried removing the if else conditions to see what is working and what is not.

$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) {
//var_dump($cat); echo '<br>';

    $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 );

    foreach ($sub_categories as $subcat) {
        $sub_categories = $sub_categories.$subcat->name.',';
    }

    $sub_categories = rtrim($sub_categories);

The code does retrieves the parent category and ALL the sub categories for ALL top level categories. Though I want the sub-categories for a particular parent category. I tried parent=>$cat->term_id and $cat->term_taxonomy_id. Still the results were same. I tried hardcoding the parent's term_id but to no use.

Okay, so my first task is to get the subcategories for a particular category!

And my second question is that why the if condition( marked in orange) doesn't work well ?

Please let me know what modification I have to make here !

closed as off-topic by Mayeenul Islam, Robert hue, s_ha_dum Apr 30 '15 at 23:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • one question: in the $args2, what is stored in the $ids of 'include' => $ids,? and maybe also the other variables? does this help? $args2 = array('parent'=>$cat->term_id); – websupporter Apr 16 '15 at 18:35
  • @websupporter Actually this has been referred across on the same forum. And I have used these variables as such. Seems like woocommerce has built in values for these or maybe these are passed empty. Even I was confused first but it worked. In short you need not worry about these variables as they successfully lists TOP level category products. – kushalvm Apr 16 '15 at 18:46
0

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';
        }
    }
}
?>
  • My subcategories shows but for all parent categories. Mentioned this under debug results section – kushalvm Apr 16 '15 at 19:14
  • so, you want it just for sub-terms of the cat with the slug 'essays'. added some more code. does it help? – websupporter Apr 16 '15 at 19:17
0

The problem was the else condition. I removed it and the code works fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.