*

LeeNewman

  • **
  • 13 posts
Hide empty categories in search-sidebar.php
« on: February 22, 2023, 07:58:04 AM »
Hi - I would like to hide empty categories from the search sidebar. The search-sidebar.php calls the script below from the theme functions.php but I don't know how to modify it so it only shows categories with one or more entry in. Does anyone here know how to do this?

function sigma_sidebar_category_search($catId = null)
{
  $aCategories = array();
  if($catId==null || $catId <= 0) {
    $aCategories[] = Category::newInstance()->findRootCategoriesEnabled();
  } else {
    // if parent category, only show parent categories
    $aCategories = Category::newInstance()->toRootTree($catId);
    end($aCategories);
    $cat = current($aCategories);
    // if is parent of some category
    $childCategories = Category::newInstance()->findSubcategoriesEnabled($cat['pk_i_id']);
    if(count($childCategories) > 0) {
      $aCategories[] = $childCategories;
    }
  }

  if(count($aCategories) == 0) {
    return "";
  }

  sigma_print_sidebar_category_search($aCategories, $catId);
}

function sigma_print_sidebar_category_search($aCategories, $current_category = null, $i = 0)
{
  $class = '';
  if(!isset($aCategories[$i])) {
    return null;
  }

  if($i===0) {
    $class = 'class="category"';
  }

  $c   = $aCategories[$i];
  $i++;
  if(!isset($c['pk_i_id'])) {
    echo '<ul '.$class.'>';
    if($i==1) {
      echo '<li><a href="'.osc_esc_html(osc_update_search_url(array('sCategory'=>null, 'iPage'=>null))).'">'.__('All categories', 'sigma')."</a></li>";
    }
    foreach($c as $key => $value) {
  ?>
      <li>
        <a id="cat_<?php echo osc_esc_html($value['pk_i_id']);?>" href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=> $value['pk_i_id'], 'iPage'=>null))); ?>">
        <?php if(isset($current_category) && $current_category == $value['pk_i_id']){ echo '<strong>'.$value['s_name'].'</strong>'; }
        else{ echo $value['s_name']; } ?>
        </a>

      </li>
  <?php
    }
    if($i==1) {
    echo "</ul>";
    } else {
    echo "</ul>";
    }
  } else {
  ?>
  <ul <?php echo $class;?>>
    <?php if($i==1) { ?>
    <li><a href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=>null, 'iPage'=>null))); ?>"><?php _e('All categories', 'sigma'); ?></a></li>
    <?php } ?>
      <li>
        <a id="cat_<?php echo osc_esc_html($c['pk_i_id']);?>" href="<?php echo osc_esc_html(osc_update_search_url(array('sCategory'=> $c['pk_i_id'], 'iPage'=>null))); ?>">
        <?php if(isset($current_category) && $current_category == $c['pk_i_id']){ echo '<strong>'.$c['s_name'].'</strong>'; }
            else{ echo $c['s_name']; } ?>
        </a>
        <?php sigma_print_sidebar_category_search($aCategories, $current_category, $i); ?>
      </li>
    <?php if($i==1) { ?>
    <?php } ?>
  </ul>
<?php
  }
}

*

LeeNewman

  • **
  • 13 posts
Re: Hide empty categories in search-sidebar.php
« Reply #1 on: February 22, 2023, 11:05:50 AM »
OK. I have found a solution to this, although not ideal as it means creating a new public function in category.php


  function findSubcategoriesNotEmpty($categoryID)
  {
   // Custom function to hide empty categories
    // $this->dao->where( 'fk_i_parent_id', (int)($categoryID));
    // $this->dao->where( 'a.b_enabled', '1' );
    return $this->listWhere('a.fk_i_parent_id = %s AND a.b_enabled = 1 AND c.i_num_items > 0', (int)$categoryID);
  }


Then find this line in the theme function.php file and change this line:

$childCategories = Category::newInstance()->findSubcategoriesEnabled($cat['pk_i_id']);

to this:

$childCategories = Category::newInstance()->findSubcategoriesNotEmpty($cat['pk_i_id']);