Forums / Setting Up Categories, Products, Attributes / How to hide categories with no active products?

How to hide categories with no active products?

Views: 26,152

Results 81 to 83 of 83
5 Sep 2018, 1:00 AM
#81
net3431 avatar

net3431

New Zenner

Join Date:
Dec 2008
Location:
Phoenix, AZ
Posts:
28
Plugin Contributions:
0

How to hide categories with no active products?

Unfortunately, the code changes did not hide the categories with no products. When the switch is set to 1, I can still follow the menu to:

"There are no products to list in this category."

I suppose I need to hack some menu code to make that happen...

5 Sep 2018, 2:05 AM
#82
mc12345678 avatar

mc12345678

Totally Zenned

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

Re: How to hide categories with no active products?

net3431:

Unfortunately, the code changes did not hide the categories with no products. When the switch is set to 1, I can still follow the menu to:

"There are no products to list in this category."

I suppose I need to hack some menu code to make that happen...
With absence of detail, not sure which code chnages have been applied. The recommended code changes for ZC 1.5.5 were provided by the all wonderful Ajeh back in this post: https://www.zen-cart.com/showthread.php?174342-How-to-hide-categories-with-no-active-products&p=1265047#post1265047

Perhaps some post after offered further guidance, I did not look beyond that in my own short sightedness.

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

6 May 2020, 8:27 PM
#83
mikeel100 avatar

mikeel100

Zen Follower

Join Date:
Sep 2012
Location:
West Jefferson, NC
Posts:
391
Plugin Contributions:
0

Re: How to hide categories with no active products?

If you do not want to booger up your Zencart files and keep track of multiple files changing, run this code:

//HIDE CATEGORIES AND SUBCATEGORIES WITH NO ACTIVE PRODUCTS
$distict_cIDs = $db->execute("select distinct `categories_id` from `" . TABLE_CATEGORIES . "` where `categories_id` not in (select `parent_id` from `" . TABLE_CATEGORIES . "`) ");
foreach ($distict_cIDs as $ids) {
  $distinct_cID = $distict_cIDs->fields['categories_id'];
  $mcid_to_status_count = $db->execute("SELECT COUNT(`master_categories_id`) AS `NumberOfProducts` FROM `" . TABLE_PRODUCTS . "` WHERE `master_categories_id` = $distinct_cID and `products_status` = 1 ");
  $count = $mcid_to_status_count->fields['NumberOfProducts'];
  if ($count == 0) {
    $db->execute("update " . TABLE_CATEGORIES . " set `categories_status` = 0 where `categories_id` = $distinct_cID ");
  }
}
//INCREASE < 3 AS NEEDED...THIS CODE, WITH THE ONE ABOVE WILL TAKE CARE OF 4 CATEGORIES/SUBCATEGORIES DEEP
$i = 0;
while ($i < 3) {
  $distict_cIDs = $db->execute("select distinct `categories_id` from `" . TABLE_CATEGORIES . "` where `categories_id` in (select `parent_id` from `" . TABLE_CATEGORIES . "`) ");
  foreach ($distict_cIDs as $ids) {
    $distinct_cID = $distict_cIDs->fields['categories_id'];
    $associated_subcategories_count = $db->execute("SELECT COUNT(`categories_id`) AS `AssociatedSubcategoriesCount` FROM `" . TABLE_CATEGORIES . "` WHERE `parent_id` = $distinct_cID ");
    $subcategories_count = $associated_subcategories_count->fields['AssociatedSubcategoriesCount'];
    $associated_subcategories_count_status_0 = $db->execute("SELECT COUNT(`categories_id`) AS `SubcategoriesStatus0Count` FROM `" . TABLE_CATEGORIES . "` WHERE `parent_id` = $distinct_cID and `categories_status` = 0 ");
    $status_2_count = $associated_subcategories_count_status_0->fields['SubcategoriesStatus0Count'];
    if ($subcategories_count == $status_2_count) {
      $db->execute("update " . TABLE_CATEGORIES . " set `categories_status` = 0 where `categories_id` = $distinct_cID ");
    }
  }
  $i++;
}

I run it every time I check for supplier updates (twice a day).

Ideally, plug it into the end of the ordering process so that it runs every time an order is completed.

If you are smart enough to turn it into a "watcher class" please do and share with everyone.