Magento按管理顺序加载类别


Magento Load Categories in Admin order

我正试图以与管理面板中相同的顺序加载标记为包含在Magento导航中的所有类别,以构建自定义分层菜单(我们将输出与存储在数据库中的另一个菜单组合到其他页面)。

这就是我目前在生成菜单的功能中使用的内容:

private function generateCategories() {
    $_root_category_id = Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId();
    $_current_children = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addIsActiveFilter()
        ->addLevelFilter(2)
        ->addOrderField('position', 'asc');
    $i = 0;
    $html = '';
    foreach( $_current_children as $l0 ) {
        if ($l0->getID() != $_root_category_id && $l0->getName() != '' && $l0->getIncludeInMenu())  {
            $i++;
            if (Mage::helper('core/url')->getCurrentUrl() == $l0->getURL())
                $active = ' active';
            else
                $active = '';
            if ($l0->hasChildren())
                $parent = ' parent';
            else
                $parent = '';
            $html .= '<li class=" level0' . $active . $parent . '"><a href="' . $l0->getURL() . '" class="top">' . $l0->getName() . '</a>';
            if ($l0->hasChildren()) {
                $multiplier = 1;
                $iteration = 0;
                $level1 = '';
                $level1[] = '';
                foreach ( explode(',', $l0->getChildren()) as $l1 ) {
                    $l1 = Mage::getModel('catalog/category')->load($l1);
                    if ( $l1->getIncludeInMenu() ) {
                        if (Mage::helper('core/url')->getCurrentUrl() == $l1->getURL())
                            $active = ' active';
                        else
                            $active = '';
                        if ($iteration == $this->perColumn) {
                            $iteration = 0;
                            $multiplier++;
                        }
                        $iteration++;
                        $level1[] = '<span class="level1' . $active . '"><a href="' . $l1->getURL() . '" title="' . $l1->getName() . '">' . $l1->getName() . '</a></span>';
                    }
                }
                unset($level1[0]);
                $numLinks = count($level1);
                $columns = $numLinks / $this->perColumn;
                $html .= '<div class="border-cover"></div><div class="dropdown" style="width: ' . $this->colWidth * $multiplier . 'em;">';
                $used = 0;
                $iteration = 0;
                foreach( $level1 as $link ) {
                    $used++;
                    $iteration++;
                    if ($used == 1)
                        $html .= '<div class="col" style="float: left; width: ' . $this->colWidth . 'em;">';
                    $html .= $link;
                    if ($used == 4 || $iteration == $numLinks) {
                        $html .= '</div>';
                        $used = 0;
                    }
                }
                $html .= '</div>';
            }
            $html .= '</li>';
        }
    }
    return $html;
}

我的印象是->addOrderField('position', 'asc')应该按照与Admin面板中相同的顺序过滤类别,但这只适用于一级($l0)类别,而不适用于子类别。

有人能建议如何修改以使其发挥作用吗?

尝试使用函数getChildrenCategories()而不是getChildren,这将返回一个对象,而不是一个类别的id,这样您就不需要加载信息了。

更多信息可以在Magento子类别的更改排序顺序中找到

$subcategories=Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id',$categoryId)
->addAttributeToSort("名称",ASC)
?>