Magento:以与admin后端树结构相同的顺序显示子子类别

时间:2012-12-21 11:47:51

标签: php zend-framework magento magento-1.7 magento-1.6

我使用的是Magento 1.7.2,并且显示了当前类别中子类别和子子类别的列表。我的问题是子子类别与管理后端树结构中的顺序不同。看起来他们是通过他们的id订购的,从较小的id号到最高号。我需要它们显示,因为它们是在管理后端订购的。这是我目前的代码:

<?php 
$currCat = Mage::registry('current_category');
$parentname = $currCat->getName();
$collection = Mage::getModel('catalog/category')->getCategories($currCat->getEntityId());
$subcats = $currCat->getChildren();

$_helper = $this->helper('catalog/output');
echo '<h2 class="titleCat"><strong>'.$parentname.'</strong></h2>';
?>

<?php 
$currCat = Mage::registry('current_category');
$parentname = $currCat->getName();
$collection = Mage::getModel('catalog/category')->getCategories($currCat->getEntityId());
$subcats = $currCat->getChildren();

$_helper = $this->helper('catalog/output');
echo '<h2 class="titleCat"><strong>'.$parentname.'</strong></h2>';
?>

<!-- We list sub sub categories -->
<div class="colLeftNav">
<ul class="colLeftSubCats">
<?php
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
$sub_subcats = $sub_cat->getChildren();
echo '<div class="subMainCat"><a href="'.$_category->getURL().'" title="Show products "'.$_category->getName().'" category">'.$_category->getName().'</a></div>';
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="subCat"><a href="'.$_sub_category->getURL().'" title="show products "'.$_sub_category->getName().'" category">'.$_sub_category->getName().'</a></li>';
}
}
}
}
?>
</ul>
</div>

我几乎被困在这里并且不知道如何解决这个问题。任何帮助都会非常感激......!

1 个答案:

答案 0 :(得分:1)

这样的东西?

$cat_id = 10;
$category = Mage::getModel('catalog/category')->load($cat_id);
$collection = Mage::getModel('catalog/category')->getCategories($cat_id, 0, true, true);

foreach ($collection as $cat) {
    echo $cat->getId().' '.$cat->getPosition().' '.$cat->getName().'<br/>';
}