Hide Categories with no products / turn categories off
- 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
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?
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?
-
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
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();
}
}- 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
@ecartzecartz wrote: ↑Sun Jan 08, 2023 1:51 pmNote that some things create their own category_tree, and those would not be affected.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(); } }
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
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.
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.
- 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
Ok that makes sense.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.
The hook generates this error
on this linePHP Fatal error: Cannot use temporary expression in write context
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
- 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
unfortunately that did not workOmar_one wrote: ↑Sun Jan 08, 2023 4:15 pm @tessthepup I think there is missing $
try thisCode: Select all
unset($GLOBALS['category_tree']::$_parents[$category_id]);
-
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
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.
- 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
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
I updated the code again (to remove the assignment in the hook listener).tessthepup wrote: ↑Sun Jan 08, 2023 6:54 pm now all categories are hidden whether they are status of 1 or 0