player.c64 Posted June 10, 2013 Share Posted June 10, 2013 Hi everybody, I need to display on main category page all sub categories including sub sub categories See on attached visual. I know it can be done by manipulating category.tpl. Trying to use syntax below but doesn't work {if isset($categoriesTree.children)} {foreach $categoriesTree.children as $child} {if $child@last} {include file="$tpl_dir./category-tree-branch.tpl" node=$child last='true'} {else} {include file="$tpl_dir./category-tree-branch.tpl" node=$child} {/if} {/foreach} {/if} Many thanks for all help. Link to comment Share on other sites More sharing options...
NemoPS Posted June 10, 2013 Share Posted June 10, 2013 Hi, I believe sub-subs are not retrieved in the category page. Only subcategories. . You might want to extend the category controller and edit the following method: protected function assignSubcategories() { if ($subCategories = $this->category->getSubCategories($this->context->language->id)) { $this->context->smarty->assign(array( 'subcategories' => $subCategories, 'subcategories_nb_total' => count($subCategories), 'subcategories_nb_half' => ceil(count($subCategories) / 2) )); } } Just use a foreach and grab subcategories for these too Link to comment Share on other sites More sharing options...
player.c64 Posted June 10, 2013 Author Share Posted June 10, 2013 (edited) Thanks Nemo for your reply. How to modify categoryController, finally how to received them in category.tpl? Edited June 10, 2013 by player.c64 (see edit history) Link to comment Share on other sites More sharing options...
NemoPS Posted June 10, 2013 Share Posted June 10, 2013 If you don't know how to use overrides you can change that functions directly. Inside the IF, add foreach $subCategories as $key ->$subCat. THen inside ther foreach loop, create a new category object with the subcategory ID, then use the same function prestashop does (getSubcategory) on that object, and save it to $subCategories[$key] Link to comment Share on other sites More sharing options...
player.c64 Posted June 10, 2013 Author Share Posted June 10, 2013 Can you give me an examples I'm not familiar with smarty. Many thanks. Link to comment Share on other sites More sharing options...
NemoPS Posted June 10, 2013 Share Posted June 10, 2013 No no this is not to be done inside a template, but in categorycontroller.php, only php knowledge is required Haven't tested it, but here it is protected function assignSubcategories() { if ($subCategories = $this->category->getSubCategories($this->context->language->id)) { foreach ($subCategories as $key => $subcat) { $subcatObj = new Category($subcat['id_category']); $subcategories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id)); } $this->context->smarty->assign(array( 'subcategories' => $subCategories, 'subcategories_nb_total' => count($subCategories), 'subcategories_nb_half' => ceil(count($subCategories) / 2) )); } } 1 Link to comment Share on other sites More sharing options...
upsilon Posted July 4, 2013 Share Posted July 4, 2013 Hello ! After a long search I came across your post but it does not work :-/ I tried to change but to no avail: '(Any ideas why? What I want to do: on the page of categories I am looking to display the sub & sub-sub categories Thank you very much! (Excuse me for my English I'm french and i use google translate) Link to comment Share on other sites More sharing options...
neller Posted July 23, 2013 Share Posted July 23, 2013 Has anyone been successful in getting this to work as intended? Link to comment Share on other sites More sharing options...
R.Kinkeris Posted September 19, 2013 Share Posted September 19, 2013 This worked for me.in CategoryController.php override -> <?php class CategoryController extends CategoryControllerCore { public function setMedia() { $this->context->controller->addJs(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js'); $this->context->controller->addJqueryUi('ui.accordion'); } protected function assignSubcategories() { if ($subCategories = $this->category->getSubCategories($this->context->language->id)) { foreach ($subCategories as $key => $subcat) { $subcatObj = new Category($subcat['id_category']); $subCategories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id); } $this->context->smarty->assign(array( 'subcategories' => $subCategories, 'subcategories_nb_total' => count($subCategories), 'subcategories_nb_half' => ceil(count($subCategories) / 2) )); } } } and in tpl file -> {foreach from=$subcategories item=subcategory} <h3>{$subcategory.name|escape:'htmlall':'UTF-8'}</h3> <div> <ul class="sub-menu"> {foreach from=$subcategory['subcategories'] item=subcategories} <li> <a href="{$link->getCategoryLink($subcategories.id_category, $subcategories.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategories.name|escape:'htmlall':'UTF-8'}</a> </li> {/foreach} </ul> </div> {/foreach} And now I have nice accardion type menu. Link to comment Share on other sites More sharing options...
klubas Posted October 17, 2013 Share Posted October 17, 2013 Not working on v.1.5 or I'm not doing it correctly. Need more explanation Link to comment Share on other sites More sharing options...
R.Kinkeris Posted October 17, 2013 Share Posted October 17, 2013 Which part exactly isn't working? Did you delete class_index.php in cache folder after you made the override? Link to comment Share on other sites More sharing options...
klubas Posted October 17, 2013 Share Posted October 17, 2013 Hello, I replaced all content in: ..\controllers\front\CategoryController.php with: <?php class CategoryController extends CategoryControllerCore { public function setMedia() { $this->context->controller->addJs(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js'); $this->context->controller->addJqueryUi('ui.accordion'); } protected function assignSubcategories() { if ($subCategories = $this->category->getSubCategories($this->context->language->id)) { foreach ($subCategories as $key => $subcat) { $subcatObj = new Category($subcat['id_category']); $subCategories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id); } $this->context->smarty->assign(array( 'subcategories' => $subCategories, 'subcategories_nb_total' => count($subCategories), 'subcategories_nb_half' => ceil(count($subCategories) / 2) )); } } } and in category.tpl replaced this: {foreach from=$subcategories item=subcategory} <li class="clearfix"> <a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" title="{$subcategory.name|escape:'htmlall':'UTF-8'}" class="img"> {if $subcategory.id_image} <img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html'}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> {else} <img src="{$img_cat_dir}default-medium_default.jpg" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> {/if} </a> <a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" class="cat_name">{$subcategory.name|escape:'htmlall':'UTF-8'}</a> {if $subcategory.description} <p class="cat_desc">{$subcategory.description}</p> {/if} </li> {/foreach} with this: {foreach from=$subcategories item=subcategory} <h3>{$subcategory.name|escape:'htmlall':'UTF-8'}</h3> <div> <ul class="sub-menu"> {foreach from=$subcategory['subcategories'] item=subcategories} <li> <a href="{$link->getCategoryLink($subcategories.id_category, $subcategories.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategories.name|escape:'htmlall':'UTF-8'}</a> </li> {/foreach} </ul> </div> {/foreach} Please correct me if this is wrong. Thank you. Link to comment Share on other sites More sharing options...
R.Kinkeris Posted October 17, 2013 Share Posted October 17, 2013 Did you override the controller correctly? I think you did wrong at the first part. Don't replace all content in in YOURSITE/controllers/front/CategoryController.php. This is what you have to do:1. Undo ALL the things you did in YOURSITE/controllers/front/CategoryController.php 2. Navigate to YOURSITE/override/controllers/front and create file CategoryController.php 3. In that file place the script <?php class CategoryController extends CategoryControllerCore { public function setMedia() { $this->context->controller->addJs(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js'); $this->context->controller->addJqueryUi('ui.accordion'); } protected function assignSubcategories() { if ($subCategories = $this->category->getSubCategories($this->context->language->id)) { foreach ($subCategories as $key => $subcat) { $subcatObj = new Category($subcat['id_category']); $subCategories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id); } $this->context->smarty->assign(array( 'subcategories' => $subCategories, 'subcategories_nb_total' => count($subCategories), 'subcategories_nb_half' => ceil(count($subCategories) / 2) )); } } } And it should take effect. If not navigate to YOURSITE/cache and delete class_index.phpIf nothing happens please be sure that you have created category with subcategories. P.S. post any error messages if you get them. Link to comment Share on other sites More sharing options...
fulviods Posted May 29, 2014 Share Posted May 29, 2014 Hello!I can't make it work on PS 1.6.0.6Any idea? The function setMedia seem not to work, and in the category.tpl the html code is not the same...Could be possible to have an update? thanks a lot Link to comment Share on other sites More sharing options...
dformica Posted November 23, 2015 Share Posted November 23, 2015 Anybody got this working in 1.6? Link to comment Share on other sites More sharing options...
Daelune Posted November 24, 2015 Share Posted November 24, 2015 Sorry to dig up old post. Managed to get this working in the latest Prestashop (1.6) by modifying the controller slightly. My override/controllers/front/CategoryController.php: class CategoryController extends CategoryControllerCore { protected function assignSubcategories() { if ($sub_categories = $this->category->getSubCategories($this->context->language->id)) { foreach ($sub_categories as $key => $subcat) { $subcatObj = new Category($subcat['id_category']); $sub_categories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id); } $this->context->smarty->assign(array( 'subcategories' => $sub_categories, 'subcategories_nb_total' => count($sub_categories), 'subcategories_nb_half' => ceil(count($sub_categories) / 2) )); } } } When you have this code in, delete the class_index.php cache file. Then just lump the code provided by Klubas into your category.tpl file in your theme somewhere within your $subcategory loop. I've posted it here for convenience. <ul class="sub-menu"> {foreach from=$subcategory['subcategories'] item=subcategories} <li> <a href="{$link->getCategoryLink($subcategories.id_category, $subcategories.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategories.name|escape:'htmlall':'UTF-8'}</a> </li> {/foreach} </ul> Link to comment Share on other sites More sharing options...
dformica Posted November 24, 2015 Share Posted November 24, 2015 Sorry to dig up old post. Managed to get this working in the latest Prestashop (1.6) by modifying the controller slightly. My override/controllers/front/CategoryController.php: class CategoryController extends CategoryControllerCore { protected function assignSubcategories() { if ($sub_categories = $this->category->getSubCategories($this->context->language->id)) { foreach ($sub_categories as $key => $subcat) { $subcatObj = new Category($subcat['id_category']); $sub_categories[$key]['subcategories'] = $subcatObj->getSubCategories($this->context->language->id); } $this->context->smarty->assign(array( 'subcategories' => $sub_categories, 'subcategories_nb_total' => count($sub_categories), 'subcategories_nb_half' => ceil(count($sub_categories) / 2) )); } } } When you have this code in, delete the class_index.php cache file. Then just lump the code provided by Klubas into your category.tpl file in your theme somewhere within your $subcategory loop. I've posted it here for convenience. <ul class="sub-menu"> {foreach from=$subcategory['subcategories'] item=subcategories} <li> <a href="{$link->getCategoryLink($subcategories.id_category, $subcategories.link_rewrite)|escape:'htmlall':'UTF-8'}">{$subcategories.name|escape:'htmlall':'UTF-8'}</a> </li> {/foreach} </ul> Daelune, It worked perfectly! Thank you! Kisses to you. Link to comment Share on other sites More sharing options...
dformica Posted December 7, 2015 Share Posted December 7, 2015 Daelune, It worked perfectly! Thank you! Kisses to you. This script to display all subcategories stopped working after upgrading to PHP 5.5 from 5.3 Link to comment Share on other sites More sharing options...
NemoPS Posted December 9, 2015 Share Posted December 9, 2015 Is it spawning any error if you enable Dev mode? Link to comment Share on other sites More sharing options...
ZikmaSolutions Posted December 11, 2015 Share Posted December 11, 2015 Hello all, i read posts and I have same problem for my presta 1.6.1.3 (www.tech-corner.co.uk). Can some help me how to show SUB categories in main categories? Link to comment Share on other sites More sharing options...
dformica Posted December 11, 2015 Share Posted December 11, 2015 This script to display all subcategories stopped working after upgrading to PHP 5.5 from 5.3 This is no longer true, my developer must had set BO > Performance "Disable all overrides " to YES. This was the reason why my sub-categories stopped showing. The solution by Nemo1 and Daelune still works on my PS 1.6.0.13 Link to comment Share on other sites More sharing options...
NemoPS Posted December 12, 2015 Share Posted December 12, 2015 There is an option for that in the back office, it was in the theme configurator I think Link to comment Share on other sites More sharing options...
Panther.software Posted December 12, 2015 Share Posted December 12, 2015 There is an option for that in the back office, it was in the theme configurator I think Yes, but it's display subcategories only from one level deeper. Is there a chance to display subcategories from every level? Link to comment Share on other sites More sharing options...
NemoPS Posted December 14, 2015 Share Posted December 14, 2015 Ah, in this case, no. You need to code some custom module, create a custom hook at the top of the category page (the file is category.tpl), then have it get all subcategories of the current one. You need to use recursion with this$category->getSubCategories();Like for each of the subcategories, get theirs, and so on. Link to comment Share on other sites More sharing options...
Panther.software Posted December 14, 2015 Share Posted December 14, 2015 Ah, in this case, no. You need to code some custom module, create a custom hook at the top of the category page (the file is category.tpl), then have it get all subcategories of the current one. You need to use recursion with this $category->getSubCategories(); Like for each of the subcategories, get theirs, and so on. Hello Nemo1, could you provide code to such described solutions? Can be an example Link to comment Share on other sites More sharing options...
RLdru Posted March 15, 2021 Share Posted March 15, 2021 If you just want a basic array of subcat, without levels, you can just use : $subCategories = array(); $category= new Category(Tools::getValue('id_category')); if ($subCategories = $category->getSubCategories($this->context->language->id)) { foreach ($subCategories as $key => $subcat) { $subcatObj = new Category($subcat['id_category']); $subCategories= array_merge( $subCategories, $subcatObj->getSubCategories($this->context->language->id)); } } Bonne journée Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now