Page 1 of 2
Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 12:49 pm
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?
Re: Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 1:51 pm
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.
Re: Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 2:12 pm
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?
Re: Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 3:07 pm
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.
Re: Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 3:19 pm
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]);
Re: Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 4:15 pm
by Omar_one
@tessthepup I think there is missing $
Re: Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 4:19 pm
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
Re: Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 6:45 pm
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.
Re: Hide Categories with no products / turn categories off
Posted: Sun Jan 08, 2023 6:54 pm
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
Re: Hide Categories with no products / turn categories off
Posted: Mon Jan 09, 2023 12:45 am
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).