2013-02-20 74 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