Forums / Setting Up Categories, Products, Attributes / How to hide empty categories automatically?

How to hide empty categories automatically?

Views: 4,858

Results 1 to 12 of 12
23 Oct 2015, 8:42 PM
#1
miru avatar

miru

New Zenner

Join Date:
Oct 2015
Posts:
30
Plugin Contributions:
0

How to hide empty categories automatically?

Is there easy way to automatically hide category that's empty (= all products sold out)? I know I can hide them manually, but my site has lot of categories and it's rather difficult to keep track on all of them.

23 Oct 2015, 8:48 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: How to hide empty categories automatically?

Miru:

Is there easy way to automatically hide category that's empty (= all products sold out)? I know I can hide them manually, but my site has lot of categories and it's rather difficult to keep track on all of them.

While the associated links at the bottom of this page (not shown until posting) may help, the link within the first points to a solution specific to 1.5.4: https://www.zen-cart.com/showthread.php?174342-How-to-hide-categories-with-no-active-products&p=1265047#post1265047

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

24 Oct 2015, 12:56 PM
#3
miru avatar

miru

New Zenner

Join Date:
Oct 2015
Posts:
30
Plugin Contributions:
0

Re: How to hide empty categories automatically?

Thanks, I will test that.

26 Oct 2015, 5:52 PM
#4
miru avatar

miru

New Zenner

Join Date:
Oct 2015
Posts:
30
Plugin Contributions:
0

Re: How to hide empty categories automatically?

My friend said (and tested and it didn't work) that the code is made for 1.5.3 version of Zen and won't work on new one.

27 Oct 2015, 4:49 PM
#5
bislewl avatar

bislewl

Totally Zenned

Join Date:
Dec 2011
Location:
Wisconsin, USA
Posts:
674
Plugin Contributions:
12

Re: How to hide empty categories automatically?

Did you try?
Configuration -> Layout Settings -> Categories with 0 Products Status

28 Oct 2015, 8:29 AM
#6
miru avatar

miru

New Zenner

Join Date:
Oct 2015
Posts:
30
Plugin Contributions:
0

Re: How to hide empty categories automatically?

Yes, it's marked "on", but doesn't work.

28 Oct 2015, 8:26 PM
#7
bislewl avatar

bislewl

Totally Zenned

Join Date:
Dec 2011
Location:
Wisconsin, USA
Posts:
674
Plugin Contributions:
12

Re: How to hide empty categories automatically?

Miru:

Yes, it's marked "on", but doesn't work.

What template are you using sometimes certain templates override this. If you switch to classic does the problem still exist?

28 Oct 2015, 9:29 PM
#8
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: How to hide empty categories automatically?

There is a difference between turning off Categories with 0 Products and turning off Categories where there ARE Products but all are marked as products_status = 0 ... the switch works to disable the counts when the count of active products is 0 ...

I am almost done with writing one for v1.5.4 ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

28 Oct 2015, 10:40 PM
#9
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: How to hide empty categories automatically?

To have Categories automatically disappear when Products are set to products_status = 0 in Zen Cart v1.5.4, use the templates and overrides directory for the following files with the code marked in RED:
/includes/modules/categories_tabs.php

  // create link to top level category
[B]    // skip empty or status off categories
    if ((CATEGORIES_PRODUCTS_INACTIVE_HIDE == 1 && zen_count_products_in_category((int)$categories_tab->fields['categories_id']) == 0)) {
    // skip empty or status off categories
    } else {
[/B]  $links_list[] = '<a class="' . $new_style . '" href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . (int)$categories_tab->fields['categories_id']) . '">' . $categories_tab_current . '</a> ';
[B]    }[/B]
  $categories_tab->MoveNext();
}

/includes/modules/category_row.php

    //    $categories->fields['products_name'] = zen_get_products_name($categories->fields['products_id']);
[B]    if ((CATEGORIES_PRODUCTS_INACTIVE_HIDE == 1 && zen_count_products_in_category((int)$categories->fields['categories_id']) == 0)) {
    // skip empty or status off categories
    } else {
[/B]        $list_box_contents[$row][$col] = array('params' => 'class="categoryListBoxContents"' . ' ' . 'style="width:' . $col_width . '%;"',
                                             'text' => '<a href="' . zen_href_link(FILENAME_DEFAULT, $cPath_new) . '">' . zen_image(DIR_WS_IMAGES . $categories->fields['categories_image'], $categories->fields['categories_name'], SUBCATEGORY_IMAGE_WIDTH, SUBCATEGORY_IMAGE_HEIGHT) . '<br />' . $categories->fields['categories_name'] . '</a>');

      $col ++;
      if ($col > (MAX_DISPLAY_CATEGORIES_PER_ROW -1)) {
        $col = 0;
        $row ++;
      }
    [B]}[/B]
    $categories->MoveNext();
  }
}

/includes/templates/template_default/sideboxes/tpl_categories.php

  $content .= '<div id="' . str_replace('_', '-', $box_id . 'Content') . '" class="sideBoxContent">' . "\n";
  for ($i=0;$i<sizeof($box_categories_array);$i++) {
[B]    if ((CATEGORIES_PRODUCTS_INACTIVE_HIDE == 1 && $box_categories_array[$i]['count'] == 0) || zen_get_product_types_to_category($box_categories_array[$i]['path']) == 3 or ($box_categories_array[$i]['top'] != 'true' and SHOW_CATEGORIES_SUBCATEGORIES_ALWAYS != 1)) {
      // skip if this is for the document box (==3)
      // skip empty or status off categories
      continue;
    }
[/B]    switch(true) {
// to make a specific category stand out define a new class in the stylesheet example: A.category-holiday

/includes/classes/site_map.php

         $categories = $db->Execute($categories_query);
         while (!$categories->EOF) {
[B]        // skip empty or status off categories
        if (!(CATEGORIES_PRODUCTS_INACTIVE_HIDE == 1 && zen_count_products_in_category((int)$categories->fields['categories_id']) == 0)) {
[/B]           $this->data[$categories->fields['parent_id']][$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'], 'count' => 0);
[B]        }
[/B]           $categories->MoveNext();
         }
   }

And just a little below that:

   function buildBranch($parent_id, $level = 0, $parent_link = '') {
    $result = $this->parent_group_start_string;

    if (isset($this->data[$parent_id])) {
      foreach ($this->data[$parent_id] as $category_id => $category) {
[B]        if (CATEGORIES_PRODUCTS_INACTIVE_HIDE == 1 && zen_count_products_in_category((int)$category_id) == 0) {
          // skip empty or status off categories
          continue;
        }
[/B]        $category_link = $parent_link . $category_id;
        $result .= $this->child_start_string;

And to control this setting add to the database via the Tools ... Install SQL Patches:

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) VALUES ('Categories with Inactive Products Status', 'CATEGORIES_PRODUCTS_INACTIVE_HIDE', '0', 'Hide Categories with Inactive Products?<br />0= off<br />1= on', 19, 30, 'zen_cfg_select_option(array(\'0\', \'1\'), ', now());

This setting then will be found in the Configuration ... Layout Settings ...

Categories with Inactive Products Status
Hide Categories with Inactive Products?
0= off
1= on

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

24 Mar 2019, 1:58 AM
#10
iatia avatar

iatia

Zen Follower

Join Date:
Nov 2009
Location:
Aransas Pass, TX
Posts:
102
Plugin Contributions:
1

Re: How to hide empty categories automatically?

Works great on ver 1.5.6a

Thanks!

25 Oct 2019, 12:51 PM
#11
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: How to hide empty categories automatically?

IATIA:

Works great on ver 1.5.6a

And on ZC v1.5.6c

I've spent a while trying to also hide empty categories in my menu that uses the 'categories_ul_generator' class, this code:

require_once (DIR_WS_CLASSES . 'categories_ul_generator.php');
$zen_CategoriesUL = new zen_categories_ul_generator;
$menulist = $zen_CategoriesUL->buildTree(true);
$menulist = str_replace('"level4"','"level5"',$menulist);
$menulist = str_replace('"level3"','"level4"',$menulist);
$menulist = str_replace('"level2"','"level3"',$menulist);
$menulist = str_replace('"level1"','"level2"',$menulist);
$menulist = str_replace('<li>','<li>',$menulist);
$menulist = str_replace("</li>\n</ul>\n</li>\n</ul>\n","</li>\n</ul>\n",$menulist);
echo $menulist;}

Thought I would post the solution:

/includes/classes/categories_ul_generator.php

$categories = $db->Execute($categories_query);
         while (!$categories->EOF) {
			[B]// skip empty or status off categories
			if (!(CATEGORIES_PRODUCTS_INACTIVE_HIDE == 1 && zen_count_products_in_category((int)$categories->fields['categories_id']) == 0)) {[/B]
            $this->data[$categories->fields['parent_id']][$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'], 'count' => 0);
			[B]}[/B]
            $categories->MoveNext();
        }
        

Simon

25 Oct 2019, 1:20 PM
#12
simon1066 avatar

simon1066

Totally Zenned

Join Date:
Feb 2009
Location:
UK
Posts:
1,326
Plugin Contributions:
0

Re: How to hide empty categories automatically?

I was going off Ajeh's /includes/classes/site_map.php as a guide - it looks as though there might be an additional change needed in categories_ul_generator.php, looking in to it.

Simon