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
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.
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
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
in : /app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Category/Tree.php
protected function _getInactiveItemIds($collection, $storeId)
{
----->> line 224 ----->> $filter = $collection->getAllIdsSql(); <<----------- line 224 <<< --------