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

How to hide categories with no active products?

Views: 26,086

Results 1 to 20 of 83
14 Feb 2011, 8:23 AM
#1
tj1 avatar

tj1

Totally Zenned

Join Date:
Mar 2005
Location:
California
Posts:
628
Plugin Contributions:
0

How to hide categories with no active products?

I have several categories that have zero active products. Is there some setting in Zen Cart that will automatically hide any categories that have 0 active products or will this require custom code?

I saw a setting that says Categories with 0 Products Status, but this doesn't have any effect.

I would disable to the categories manually, but this keeps changing depending on the items in stock, so this is very difficult.

Thanks for any info on this.

15 Feb 2011, 5:36 PM
#3
tj1 avatar

tj1

Totally Zenned

Join Date:
Mar 2005
Location:
California
Posts:
628
Plugin Contributions:
0

Re: How to hide categories with no active products?

According to the description for that mod, it seems to hide them when the customer chooses to hide them via a selection on a side box.

I'll download that and see how the code works, maybe I can reuse code to make it work the way I want it.

7 Jul 2013, 3:04 AM
#4
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?

I'm in the same boat...the link above to solve this issue is for v1.3. I have version 1.5.1.

I've been trying for weeks to figure out how to hide all subcategories where category counts are 0 of ### active. I have searched in the developers tool kit and looked in the database for anything related to category counts but have came up empty.

I know you can click on the green/red buttons to change the categories. I'm looking for a way to automate it. I have over 7000 products in my store and it's very time consuming to manually go through each category/subcategory to find all of the categories that are 0 of ### active and deactivate them. I don't want to leave them on because that will not give the shopper a very good experience when they click on a category and nothing is there.

I have posted this issue here http://www.zen-cart.com/showthread.php?208835-Is-there-a-way-to-turn-off-categories-and-subcategories-if-all-products-are-inactive but it's not a very active feed.

Where are category counts calculated and stored so that they will display on the admin/categories pages? That is the key I need to finish building my sql statement for taking care of this issue.

If anyone can help, I'd appreciate it.

7 Jul 2013, 2:13 PM
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,080
Plugin Contributions:
56

Re: How to hide categories with no active products?

Did you try Configuration->Layout Settings->Categories with 0 Products Status ... and set it to 0 (off)?

7 Jul 2013, 3:14 PM
#6
ajeh avatar

ajeh

Oba-san

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

Re: How to hide categories with no active products?

You could customize the code, using your templates and overrides, for the file:
/includes/templates/templates_default/sideboxes/tpl_categories.php

and add the code in RED:

     if (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)[B] or $box_categories_array[$i]['count'] == 0[/B]) {
        // skip if this is for the document box (==3)
       [B]// skip if this category has no active products[/B]
      } else {

See if that accomplishes what you are attempting ...

8 Jul 2013, 3:06 AM
#7
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?

I did try that but it has no effect.

8 Jul 2013, 3:15 AM
#8
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?

mikeel100:

I did try that but it has no effect.

Did you try Configuration->Layout Settings->Categories with 0 Products Status ... and set it to 0 (off)?

8 Jul 2013, 3:21 AM
#9
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?

Ajeh:

You could customize the code, using your templates and overrides, for the file:
/includes/templates/templates_default/sideboxes/tpl_categories.php

and add the code in RED:

 if (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)[B] or $box_categories_array[$i]['count'] == 0[/B]) {
    // skip if this is for the document box (==3)
   [B]// skip if this category has no active products[/B]
  } else {
> 
> See if that accomplishes what you are attempting ...

That made all of my categories disappear...here is the code that I modified with your suggestion. 

if (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) or $box_categories_array[$i]['count'] == 0) {
        // skip if this is for the document box (==3)
        // skip if this category has no active products MIKE
      } else {
      $content .= '<a class="' . $new_style . '" href="' . zen_href_link(FILENAME_DEFAULT, $box_categories_array[$i]['path']) . '">';
if (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) or $box_categories_array[$i]['count'] == 0) {
        // skip if this is for the document box (==3)
        // skip if this category has no active products MIKE
WHAT DO I NEED TO COMMENT OUT BELOW TO MAKE IT SKIP      } else {
      $content .= '<a class="' . $new_style . '" href="' . zen_href_link(FILENAME_DEFAULT, $box_categories_array[$i]['path']) . '">';
8 Jul 2013, 3:33 AM
#10
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?

lat9:

Did you try Configuration->Layout Settings->Categories with 0 Products Status ... and set it to 0 (off)?

It was already set to zero...besides, that is for category counts..."Show Category Count for 0 Products?"

Thanks though.

8 Jul 2013, 3:38 AM
#11
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?

Just so you know, I did copy /includes/templates/templates_default/sideboxes/tpl_categories.php
and place it in my customized folder... /includes/templates/MY_FILES/sideboxes/tpl_categories.php

8 Jul 2013, 3:40 AM
#12
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?

mikeel100:

Did you try Configuration->Layout Settings->Categories with 0 Products Status ... and set it to 0 (off)?

Yes, I'm talking to myself again...LOL. Need a break.

8 Jul 2013, 3:47 AM
#13
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?

mikeel100:

That made all of my categories disappear...here is the code that I modified with your suggestion.

if (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) or $box_categories_array[$i]['count'] == 0) {
// skip if this is for the document box (==3)
// skip if this category has no active products MIKE
} else {
$content .= '<a class="' . $new_style . '" href="' . zen_href_link(FILENAME_DEFAULT, $box_categories_array[$i]['path']) . '">';
if (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) or $box_categories_array[$i]['count'] == 0) {
// skip if this is for the document box (==3)
// skip if this category has no active products MIKE
WHAT DO I NEED TO COMMENT OUT BELOW TO MAKE IT SKIP
} else {
$content .= '<a class="' . $new_style . '" href="' . zen_href_link(FILENAME_DEFAULT, $box_categories_array[$i]['path']) . '">';

Ignore the double code above...I must have double pasted when I was replying. The code in my file is not doubled like it is above...

8 Jul 2013, 1:29 PM
#14
ajeh avatar

ajeh

Oba-san

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

Re: How to hide categories with no active products?

I forgot ... you probably have set this to false:

Show Category Counts
Count recursively how many products are in each category

Turn that to true ...

Now, starting with a clean:
/includes/templates/templates_default/sideboxes/tpl_categories.php

using your templates and overrides make the following changes in RED:

     if (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[B] or $box_categories_array[$i]['count'] == 0[/B])) {
        // skip if this is for the document box (==3)
      } else {
      $content .= '<a class="' . $new_style . '" href="' . zen_href_link(FILENAME_DEFAULT, $box_categories_array[$i]['path']) . '">';

Then change the code:

      if ([B]false && [/B]SHOW_COUNTS == 'true') {
        if ((CATEGORIES_COUNT_ZERO == '1' and $box_categories_array[$i]['count'] == 0) or $box_categories_array[$i]['count'] >= 1) {
          $content .= CATEGORIES_COUNT_PREFIX . $box_categories_array[$i]['count'] . CATEGORIES_COUNT_SUFFIX;
        }
      }

So while you say show the counts in configuration ... you will not show the counts but will still gather the counts so that categories with 0 Products will not show ...

Does this work better for you?

8 Jul 2013, 5:55 PM
#15
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?

Where, in zencart, are the calculations done for making the, ## of ## active, display next to the green/red buttons in admin/categories? That is the info I need to make a simple MySQL code work for this issue.

I've posted the post below at stackoverflow to try and help find a solution as well...http://stackoverflow.com/questions/17518068/change-category-status-to-0-in-zencart-when-all-products-in-a-category-are-zero

First off, let me say I am a NOVICE MySQL programmer. I appreciate any help I can get.

I am trying to update a record value in one table where the count of records in a separate table are equal to the count of records in another table where a certain criteria is met.

In real language: Within a specific category, if all of the products_status, in table products, are set to 0 (not active), then I want to set the category_status, in table category, to 0 (hidden).

I'll explain my thought process after I give you a little background info.

The relevant parts of my categories table are: categories_id (unique and autoincremented) categories_status (0 or 1) parent_id

The relevant parts of my products table are: products_id (autoincremented) products_status (O or 1) master_categories_id (I believe it is linked to the categories_id column n the categories table)

The products_to_categories table has two columns: products_id categories_id

Now, this is what I am attempting:

  1. Go to the first record of categories.categories_id and set that as a variable (CategorySearchID).

  2. Go to products.master_categories_id and count the number of records that match the CategorySearchID and set that to a variable (CategorySearchIDCount). This gives the total number of products in that CategorySearchID.

  3. Go to products.products_status and count all records that have a product_status of 0 where the CategorySearchID is a match.

  4. Compare the counts in step 2 and 3. If they are equal, the categories.category_status for the current CategorySearchId will be set to 0 (hidden).

  5. Finally, I need a loop that will search through all categories.category_id (CategorySearchID) and do the count comparisons and status updates in steps 2, 3 and 4.

I have tried dozens of combinations of the following code but keep getting errors. I can get some of the individual element of the code to work but I can't seem to tie them all together.

For example:

UPDATE DATABASE.categories SET categories_status = 0

Sets all category_status' to 0 successfully

SELECT COUNT(products_id) FROM products WHERE products_status=0

Finds all of the products_status' set to 0 and returns the number in a temporary table.

AND parent_id != 0;

Prevents top level categories from being set to hidden

I'm not going to bore you with all of the combinations I've tried.

Again, I really would appreciate any help!

Mike

8 Jul 2013, 6:06 PM
#16
ajeh avatar

ajeh

Oba-san

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

Re: How to hide categories with no active products?

In the Class category_tree.php the counts are made and the tree made for the sidebox ...

With only the changes that I posted, for only the tpl_categories.php file, Only Categories that contain Active Products are showing ... NOTE: you need the Category Counts turned on in the settings to know which Categories to show or exclude, but the counts will still be hidden based on the code changes that I posted ...

Have you additional changes that might be causing this to not work for you?

9 Jul 2013, 1:47 AM
#17
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?

Your a life saver and a genius Ajeh! Thanks!

I believe this will work! No need to manually change CATEGORY buttons from green to red now!

It's not affecting searches neither!

I can sleep tonight...LOL.

I'll post this answer into the other threads I ran across where nobody found a solution to.

9 Jul 2013, 3:36 AM
#18
ajeh avatar

ajeh

Oba-san

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

Re: How to hide categories with no active products?

Thanks for the update that this did work for you ... :smile:

10 Jul 2013, 2:15 AM
#19
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?

Ajeh:

Thanks for the update that this did work for you ... :smile:

No, thank you! I have ran across so many topics where people left me hanging for an answer...I didn't want to do that to anyone else. It's very frustrating. :)

10 Jul 2013, 3:18 AM
#20
ajeh avatar

ajeh

Oba-san

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

Re: How to hide categories with no active products?

Always a pleasure ... remember the Zen Cart Team when you are rich and famous! :cool: