Page 1 of 1
categories box which only shows the subcategories of selected category
Posted: Thu May 27, 2021 7:57 am
by loop
Hi All
I'm trying to show only the Subcategories (if on main page, thenn every category with parent_id=0) and on every subcategory i want to show only the childs on the left side.
i found on the old forum a entry:
(link to other service providers removed by Admin)
Raiwa suggested to put this in the bm_categories.php box (the question in the forum was for a early version, not phoenixcart). But as i can see, there is no "setRootCategoryID" function in the new tree_display.php class
Code: Select all
$OSCOM_CategoryTree->setRootCategoryID((int)$current_category_id);
any ideas how i could achieve that with the acutal classes? or do i have to overwrite the tree class?
thank you in advance
Re: categories box which only shows the subcategories of selected category
Posted: Thu May 27, 2021 9:30 am
by loop
i found a solution myself, but maybe there is a betterway. i could add a function to
"display_tree_accessor.php"
Code: Select all
public function setRootCategoryID($root_id) {
$this->tree->set_root_id($root_id);
}
and thenn from the bm_categories.php call that function with the current_categories_id, thann the navigation works, but the links are then only with the category id and not with the full cPath (which makes the breadcrump wrong

)
any help would really appreciated
a solution without changing core (or overwriting) would be great
Re: categories box which only shows the subcategories of selected category
Posted: Thu May 27, 2021 4:18 pm
by ecartz
Code: Select all
$this->tree->set_root_id($root_id);
This is a little difficult, since you aren't posting the actual calling code that you are using. But it would look something like
Code: Select all
$subcategory_tree = new category_tree();
$subcategory_tree->set_root_id($root_id);
$display = new tree_display($subcategory_tree);
Re: categories box which only shows the subcategories of selected category
Posted: Thu May 27, 2021 6:07 pm
by loop
Hi and thank you for your code, but i don't understand where you mean that...i tried it in my custom box bm_categories_subcatonly.php
Code: Select all
class bm_categories_subcatonly extends abstract_block_module {
const CONFIG_KEY_BASE = 'MODULE_BOXES_CATEGORIES_SUBCATONLY_';
function execute() {
global $current_category_id, $category_tree;
$subcategory_tree = new category_tree();
$subcategory_tree->set_root_id($current_category_id);
$display = new tree_display($subcategory_tree);
$display->setPath($GLOBALS['cPath'], '<strong>', '</strong>');
$display->setMaximumLevel((int)MODULE_BOXES_CATEGORIES_SUBCATONLY_MAX_LEVEL);
$display->setChildString('', '');
$category_name = $category_tree->get($current_category_id, 'name');
$tpl_data = ['group' => $this->group, 'file' => __FILE__];
include 'includes/modules/block_template.php';
}
protected function get_parameters() {
return [
'MODULE_BOXES_CATEGORIES_SUBCATONLY_STATUS' => [
'title' => 'Enable Categories Module',
'value' => 'True',
'desc' => 'Do you want to add the module to your shop?',
'set_func' => "tep_cfg_select_option(['True', 'False'], ",
],
'MODULE_BOXES_CATEGORIES_SUBCATONLY_CONTENT_PLACEMENT' => [
'title' => 'Content Placement',
'value' => 'Left Column',
'desc' => 'Should the module be loaded in the left or right column?',
'set_func' => "tep_cfg_select_option(['Left Column', 'Right Column'], ",
],
'MODULE_BOXES_CATEGORIES_SUBCATONLY_MAX_LEVEL' => [
'title' => 'Maximum Level of Nesting',
'value' => '1',
'desc' => 'If you increase this number, subcategories will show in the module output.',
],
'MODULE_BOXES_CATEGORIES_SUBCATONLY_SORT_ORDER' => [
'title' => 'Sort Order',
'value' => '0',
'desc' => 'Sort order of display. Lowest is displayed first.',
],
];
}
}
It works exaktly as my code (so i do not have to change core, thats good

) but the problem is still the same. All links are only to the category and not the path (index.php?cPath=579 instead of index.php?cPath=579_80) and so the breadcrump is wrong....
what i need to achieve is to have only the subtree in this box, but my "->setRootCategoryID" do that, but breadcrum and links does affect this also, so this is not the solution. Thank you for your help
Re: categories box which only shows the subcategories of selected category
Posted: Thu May 27, 2021 8:03 pm
by ecartz
loop wrote: ↑Thu May 27, 2021 6:07 pm
(index.php?cPath=579 instead of index.php?cPath=579_80) and so the breadcrump is wrong....
If you use your code but with a root ID of 0, does it show the links correctly? Obviously it will show the wrong categories in that case.
You might consider if it would be easier to create the links manually. Burt has some examples in Supporters' code.
Re: categories box which only shows the subcategories of selected category
Posted: Thu May 27, 2021 9:21 pm
by loop
yes, whenn i make root ID = 0
$subcategory_tree = new category_tree();
$subcategory_tree->set_root_id(0);
$display = new tree_display($subcategory_tree);
links are fine. with links create manually do you mean to overwrite the class "tree_display.php". At the moment i don't know what you mean with that. If you can explain or let me know where i can find examples i would really appreciate
Re: categories box which only shows the subcategories of selected category
Posted: Thu May 27, 2021 10:25 pm
by ecartz
The code that you posted does not display anything. I'm guessing that you are using a copy of the regular category box to display, which does:
By manually, I mean instead of using the default tree display, use something else.
If you want to talk about what code in the Supporters' code does similar things, you should post in the Supporters' forum.
Re: categories box which only shows the subcategories of selected category
Posted: Fri May 28, 2021 6:16 am
by loop
yeah i use the normal "category box"
i'm still trying to find out whats going on, i can't believe to make manualy the category menu only because the link is not complete (with the cPath from the root) maybe it's a small code i miss :
i found out, if i change the code in tree_display.php to:
class tree_display extends displayable_tree_accessor {
protected function _buildBranch($parent_id, $level = 0) {
$result = ((($level === 0) && ($this->parent_group_apply_to_root === true)) || ($level > 0))
? $this->parent_group_start_string
: '';
foreach ( $this->tree->get_children($parent_id) ?? [] as $id ) {
$name = $this->tree->get($id, 'name');
$link = ( $this->breadcrumb_usage === true )
? Guarantor::ensure_global('category_tree')->find_path($this->buildBreadcrumb($id))
: Guarantor::ensure_global('category_tree')->find_path($id);
it works ( i changed only the 2 bottom line, to get the full path) but don't know why the path is there not correct
but thank you anyway ecartz!