问题
I am playing with magento's home page where I have created a tab Which Shows all the categories including Root, categories, and Sub Categories (In One Tab). Now I want to Show Only Main Categories( Whose Parent Is Root) in main Tab And under Each Category I want to List Their respective Sub Categories. I wrote The following Code to achieve a part of this,
MODEL CLASS
public function getsubCategory($parent)
{
$subcategoryCollection = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToFilter('parent_id', $parent);
return $subcategoryCollection;
BLOCK CLASS
protected function b4Html_subcategory($parent)
{
$catModel = Mage::getModel('Pragtech_Sweet/category');
$mysubCategory = $catModel->getsubCategory($parent);
$this->mysubCategory = $myCategory;
return $mysubCategory;
}
TEMPLATE FILE
$obj = new Pragtech_Sweet_Block_Category();
$collection = $obj->b4Html();
foreach ($collection as $category)
{
$name = $category->getName();
$parent = $category->getParent_id();
foreach ($obj->b4Html_subcategory($parent) as $subcategory)
{
$subname = $subcategory->getName();
//Here Will Go Ther Code For Sub Categories
}
but it doesn't work.. I am unable to understand where I am doing wrong... Can anyone help me out
回答1:
Do this instead :
Mage::getModel('catalog/category')->load('23')->getChildrenCategories();
and iterate over the result.
and that's how i found it out:
$object = Mage::getModel('catalog/category');
print_r(get_class_methods($object));
print_r($object->load('23')->getChildrenCategories()->toArray());
回答2:
Here's another way to do this, if you don't want to mess with the treeModel stuff, or want more control how categories are loaded:
function getCategoryTree($root_category_name)
{
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect("*")
->addFieldToFilter("Name",array("eq"=>$root_category_name));
$stack = array();
$category = $categories->getFirstItem()->getData();
$categories=array();
array_push($stack, $category);
//regular recursion is boring, let's do a stack
while(count($stack) > 0)
{
$category = array_pop($stack);
array_push($categories, $category);
$children = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect("*")
->addFieldToFilter("parent_id",array("eq"=>$category['entity_id']))
->addAttributeToSort("position","desc");
foreach ($children as $child)
{
array_push($stack, $child->getData());
}
}
return $categories;
}
This will give you an array of categories representing the full category tree rooted at the top category with the name "Some Category Name", in order, with all of the category data. Tune this to only select the specific fields you need, instead of "*".
This technique can give you finer grained control of the loading of the category tree, and with how you want the data structured afterwards. For example, you could trivially modify this to do a hierarchical tree instead of a flat array and then serialize that to a JSON object to send to the client.
Add whatever filters you like (active, etc...) at whatever level you like to get even more control.
回答3:
iterate through this object
Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->getItems();
来源:https://stackoverflow.com/questions/7926121/how-to-get-sub-categories-in-magento