2013-02-20 76 views
2

我试图加载所有标记为包含在Magento导航中的类别,它们与管理面板中的顺序相同,以便构建自定义分层菜单(我们将输出与存储在数据库中的另一个菜单结合到其他页面)。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')要过滤的类别为相同的顺序在管理控制台中,但这仅适用于第一级($l0)类别,而不适用于子类别。

任何人都可以建议如何修改这个工作吗?

回答

3

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

更多信息可以在Change sort order of Magento subcategories

+0

谢谢,完美的作品。我没有意识到他们是一种将这些类别作为对象返回的方式,我认为他们必须在之后加载。 – 2013-02-21 08:59:12

0
$子类别=法师:: getModel( '目录/类别')中找到 - > getCollection()
- > addAttributeToSelect( '姓名')
- > addFieldToFilter( 'parent_id',$ categoryId)
- > addAttributeToSort('name',ASC);
?>
+0

这不是关于名字! – 2014-12-07 11:10:02