Hide Categories with no products / turn categories off

Open to all! Ask other shopowners for help.
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Hide Categories with no products / turn categories off

Post by tessthepup »

Has anyone come up with a solution for hiding categories or been able to turn them on/off

A very basic solution is to add a categories_status field to the categories table and then check using the query in category_tree.php but that is changing core.

Can this done be via a hook or would it be more complex?


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Hide Categories with no products / turn categories off

Post by ecartz »

Code: Select all

class enabled_category_tree extends category_tree {

  public function __construct() {
    $disabled_categories = array_column($GLOBALS['db']->fetch_all("SELECT categories_id FROM categories WHERE categories_status = 0"), 'categories_id');
    foreach ($disabled_categories as $category_id) {
      unset($GLOBALS['category_tree']->_data[$category_id]);
      unset($GLOBALS['category_tree']::_parents[$category_id]);
    }
  }

}

class hook_shop_system__29a_disabled_categories {

  public function listen_startApplication() {
    $GLOBALS['category_tree'] = new enabled_category_tree();
  }

}
Note that some things create their own category_tree, and those would not be affected.
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Hide Categories with no products / turn categories off

Post by tessthepup »

ecartz wrote: Sun Jan 08, 2023 1:51 pm

Code: Select all

class enabled_category_tree extends category_tree {

  public function __construct() {
    $disabled_categories = array_column($GLOBALS['db']->fetch_all("SELECT categories_id FROM categories WHERE categories_status = 0"), 'categories_id');
    foreach ($disabled_categories as $category_id) {
      unset($GLOBALS['category_tree']->_data[$category_id]);
      unset(category_tree::$_parents[$category_id]);
    }
  }

}

class hook_shop_system__29a_disabled_categories {

  public function listen_startApplication() {
    new enabled_category_tree();
  }

}
Note that some things create their own category_tree, and those would not be affected.
@ecartz

Thank you for your time once again. Please can you explain what this means and in particular the 29a part

class hook_shop_system__29a_disabled_categories

Also where would I actually drop the hook?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Hide Categories with no products / turn categories off

Post by ecartz »

includes/hooks/shop/system/_29a_disabled_categories.php

This would be one of the system hooks that runs from application_top.php. The _29a_ part tells it to run after _29_category_path but before anything set to run after that. I.e. _29a_ sorts after _29_ and before _30_ because a sorts after _ and 2 sorts before 3.
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Hide Categories with no products / turn categories off

Post by tessthepup »

ecartz wrote: Sun Jan 08, 2023 3:07 pm includes/hooks/shop/system/_29a_disabled_categories.php

This would be one of the system hooks that runs from application_top.php. The _29a_ part tells it to run after _29_category_path but before anything set to run after that. I.e. _29a_ sorts after _29_ and before _30_ because a sorts after _ and 2 sorts before 3.
Ok that makes sense.

The hook generates this error
PHP Fatal error: Cannot use temporary expression in write context
on this line

Code: Select all

unset($GLOBALS['category_tree']::_parents[$category_id]);
Omar_one
Senior Contributor
Posts: 679
Joined: Fri Oct 25, 2019 5:06 pm
Phoenix Version: v1.0.8.16
Has thanked: 100 times
Been thanked: 56 times

Re: Hide Categories with no products / turn categories off

Post by Omar_one »

@tessthepup I think there is missing $
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Hide Categories with no products / turn categories off

Post by tessthepup »

Omar_one wrote: Sun Jan 08, 2023 4:15 pm @tessthepup I think there is missing $
try this

Code: Select all

unset($GLOBALS['category_tree']::$_parents[$category_id]);
unfortunately that did not work
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Hide Categories with no products / turn categories off

Post by ecartz »

Omar_one wrote: Sun Jan 08, 2023 4:15 pm I think there is missing $
Yes, that is correct. It also should be referencing the class rather than the object. I updated the code in my previous post. Thanks for the reminder.
User avatar
tessthepup
Certified Developer
Posts: 383
Joined: Mon Mar 01, 2021 5:55 pm
Phoenix Version:
Has thanked: 47 times
Been thanked: 62 times

Re: Hide Categories with no products / turn categories off

Post by tessthepup »

ecartz wrote: Sun Jan 08, 2023 6:45 pm
Omar_one wrote: Sun Jan 08, 2023 4:15 pm I think there is missing $
Yes, that is correct. It also should be referencing the class rather than the object. I updated the code in my previous post. Thanks for the reminder.
Thanks @ecartz However now all categories are hidden whether they are status of 1 or 0
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Hide Categories with no products / turn categories off

Post by ecartz »

tessthepup wrote: Sun Jan 08, 2023 6:54 pm now all categories are hidden whether they are status of 1 or 0
I updated the code again (to remove the assignment in the hook listener).


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply