I need to be able to pull of a list of subcategories for a specified parent category id. For example I have two main categories setup (Departments & Flavours) on every page I want to show all of the subcategories for these main categories.
I see there is a getCurrentChildCategories function, which is kind of what I need but like this getChildCategories(4) - 4 being the parent id.
Hopefully that makes sense and what I’m asking is quite simple? I’ve searched the forum for a while and can’t find an answer.
I guess you can just copy the function, rename it to getChildCategories($parentId), and replace $category = $layer->getCurrentCategory(); with $category = Mage::getModel(’catalog/category’)->load($parentId);
I’m trying to figure out how to apply this code to a left-nav example.. Where do I put put these code snippets to have a vertical nav display the child categories, or child products directly underneath the parent category when parent is selected ?
Hi, I am trying to implement Steve’s code and it is returning a PHP error:
Warning: Invalid argument supplied for foreach() in .../httpdocs/app/design/frontend/kirna_zabete/clothes_subcats/template/catalog/navigation/left.phtml on line 35
in my template folder in app/design/frontend/default/mytheme/template/catalog/navigation I created a new file called: vert_nav.phtml
and referenced this file in my catelog.xml file
<?php /** * Magento * * NOTICE OF LICENSE * * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/osl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@magentocommerce.com so we can send you a copy immediately. * * @category design_default * @package Mage * @copyright Copyright (c) 2004-2007 Irubin Consulting Inc. DBA Varien (http://www.varien.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ ?> <? /** * Category left navigation * * @see Mage_Catalog_Block_Navigation */ ?> <? $_categories=$this->getCurrentChildCategories() ?> <?if($_categories->count()):?>
<?else: //No sub categories? Get the parent category and show that menu instead $_currentCateogry = Mage::registry('current_category'); if ($_currentCateogry) {
foreach ($pathIds as $categoryId) { if ($categories[$categoryId]->getName() != Mage::registry('current_category')->getName()){ $_parentCateogry = $categories[$categoryId]; //echo $_parentCateogry;
//we grab the id of the parent category with the next line $id=Mage::getBlockSingleton('catalog/navigation')->getCurrentCategory()->parent_id; $_categories=$this->getSubCategories($id);
then I added the following code to app/code/core/Mage/Catalog/Block/Navigation.php
//get subcategories public function getSubCategories($parentId) { $helper = Mage::helper('catalog/category'); return $helper->getSubCategories($parentId); }
I also added the following code to app/code/core/Mage/Catalog/Model/Category.php
//get the category parent id public function getSubCategories($parentId) { $helper = Mage::helper('catalog/category'); return $helper->getSubCategories($parentId); }
and I also added the following code to app/code/core/Mage/Catalog/Helper/Category.php
//get category parent id public function getSubCategories($parentId, $sorted=false, $asCollection=false, $toLoad=true) { $category = Mage::getModel('catalog/category'); /* @var $category Mage_Catalog_Model_Category */ if (!$category->checkId($parentId)) { if ($asCollection) { return new Varien_Data_Collection(); } return array(); }
This solution prints out the sub categories of my parent category and it’s working sweet!
Just remember to make the changes in the local folder so that your changes won’t get overwrite with the next upgrade
I tried this technique and it worked beautifully. Thanks for posting it.
Following the recommendation in the last post, I extended the core classes, and created directories for them in my /app/code/local directory
This resulted in the following files:
/app/code/local/[Foo]/Catalog/Block/Navigation.php
/app/code/local/[Foo]/Catalog/Helper/Category.php
/app/code/local/[Foo]/Catalog/Model/Category.php
With the following class names:
Foo_Catalog_Block_Navigation
Foo_Catalog_Helper_Category
Foo_Catalog_Model_Category
The extended classes are very simple, only containing the additional methods, like so:
/** * Catalog navigation * * @category Foo * @package Foo_Catalog * @author You */ class Foo_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation {
/** * Get sub categories by parent ID * * @param int $parentId * @return void * @author You */ public function getSubCategories($parentId) { $helper = Mage::helper('catalog/category'); return $helper->getSubCategories($parentId); }
Update: I added the other two classes to my code samples above, in case anyone wants to copy them. Remember “Foo” should be the namespace for your project, so the parent directory “Foo” should be renamed as well as the first part of each class name.
any way to extend this script to print out sub categories in a separate <ul> so that we can use it with jQuery accordion?
any help would be much appreciated,
You may want to look at the Mage_Catalog_Block_Navigation::drawItem() method - and creating a new method based upon it - to render out an unordered list which highlights the current category by css class name.
See [theme folder]/template/catalog/navigation/top.phtml for usage.
That should provide you with enough HTML / CSS hooks to let jQuery expand or collapse the resulting menu.
This is excellent guys! Thanks for sharing this code. I had to write up the config.xml myself but that wasn’t so hard to figure out after following the examples here…
Also I think I remember reading in another thread that you can’t extend the same class twice with two different extensions. Without looking that up to see if I was 100% correct, and knowing that Rico’s VertNav extension already extends Mage_Catalog_Block_Navigation, I just added the new public function for the Block extension in his existing file. This would probably give me trouble after upgrading his extension but I am sure I can quickly add it again.
It errors out in getSubCategories function at:
$tree->addCollectionData(null, $sorted, $parentId, $toLoad, true);
with an error of:
PHP Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat_Collection::getAllIdsSql() in /Applications/MAMP/htdocs/magento/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Category/Tree.php on line 224
Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Flat_Collection::getAllIdsSql() in /var/clients/client7/web9/web/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Category/Tree.php on line 224