2012-05-07 90 views
-2

category list在Magento從兒童類生成樹

我有孩子的類別名稱及其ID

所示的圖像

前馬紮裏 - 沙里夫和赫拉特

怎麼能我如圖所示從子類別生成一棵樹?

我要生成樹相同的圖像

+0

想,如果你期望社區解決您的任務,那麼你也應該告訴我們多少你要爲此付出代價,以生成樹相同的圖像 –

+0

。 –

回答

1

使用下面的代碼,它會幫助你。

$_rootCatId  = 2; 
$_rootCategory = Mage::getModel('catalog/category')->load($_rootCatId); 
$_catName  = $_rootCategory->getName(); 
if($_rootCategory->hasChildren()) 
{ 
$_collection = Mage::getResourceModel('catalog/category_collection') 
       ->addAttributeToFilter('is_active', 1) 
       ->addAttributeToSort('position', 'asc') 
       ->joinUrlRewrite() 
       ->addIdFilter($_rootCategory->getChildren()) 
       ->load(); 

foreach($_collection AS $_sub) 
{ 
    $_subCat = Mage::getModel('catalog/category')->load($_sub->getId()); 

    if($_subCat->hasChildren()) 
    { 
     echo '<li class="leve10 nav-'. $_subCat->getId() .'">'; 
      echo '<a id="subCatLink">'; 
       echo '<span>'. $this->htmlEscape($_subCat->getName()) .'</span> 
       </a> 
     </li>'; 
     echo '<ul id="subCatUl" style="padding:0px 13px;">'; 
     $__collection = Mage::getResourceModel('catalog/category_collection') 
       ->addAttributeToFilter('is_active', 1) 
       ->addAttributeToSort('position', 'asc') 
       ->joinUrlRewrite() 
       ->addIdFilter($_subCat->getChildren()) 
       ->load(); 

     foreach($__collection AS $__sub) 
     { 
      $__subCat = Mage::getModel('catalog/category')->load($__sub->getId()); 
      echo '<li class="leve20 nav-'. $__subCat->getId() .'"> 
        <a href="'. $this->getCategoryUrl($__subCat) .'"> 
        <span>'. $this->htmlEscape($__subCat->getName()) .'</span> 
        </a> 
       </li>'; 
     } 
     echo '</ul>'; 
    } 
    else 
    { 
     echo '<li class="leve10 nav-'. $_subCat->getId() .'"> 
      <a href="'. $this->getCategoryUrl($_subCat) .'"> 
      <span>'. $this->htmlEscape($_subCat->getName()) .'</span> 
      </a> 
     </li>'; 
    } 
} 
}