如何在Magento中获取子类别? [英] How To Get Sub Categories in Magento ?`
本文介绍了如何在Magento中获取子类别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在玩magento的主页,在那里我创建了一个选项卡,该选项卡显示所有类别,包括根,类别和子类别(在一个选项卡中).现在,我只想在主选项卡中和每个类别下仅显示主要类别(其父母是谁),以列出他们各自的子类别.我编写了以下代码以实现其中的一部分,
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,
模型类
public function getsubCategory($parent)
{
$subcategoryCollection = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToFilter('parent_id', $parent);
return $subcategoryCollection;
块类
protected function b4Html_subcategory($parent)
{
$catModel = Mage::getModel('Pragtech_Sweet/category');
$mysubCategory = $catModel->getsubCategory($parent);
$this->mysubCategory = $myCategory;
return $mysubCategory;
}
模板文件
$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
推荐答案
请改为执行此操作:
Mage::getModel('catalog/category')->load('23')->getChildrenCategories();
并遍历结果.
这就是我发现它的方式:
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());
这篇关于如何在Magento中获取子类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文