2013-08-16 49 views
0

想顯示所有的第二級類別的URL和我的代碼是如何獲得在Magento的類別

<?php $storeId = Mage::app()->getStore()->getId(); 

//Get category model 
$_category = Mage::getModel('catalog/category')->setStoreId($storeId); 

$_categoryCollection = $_category->getCollection(); 
$_categoryCollectionIds = $_categoryCollection->getAllIds(); 

//Remove root category from array 
unset($_categoryCollectionIds[0], $_categoryCollectionIds[1]); 

?> 

<div id="accordian_hover"> 
<?php 

$o = null; 
$o .= '<ul>'; 

foreach ($_categoryCollectionIds as $catId) { 

    $_category = $_category->load($catId); 

     if($_category->getLevel() == 2) { 

      $catChildren = $_category->getChildren();   

       if(!empty($catChildren)) { 
        $o .= '<li> <a href="'.$_category->getUrl().'">'.$_category->getName().'</a>'; 
        $o .= '<ul>'; 

        $categoryChildren = explode(",", $catChildren); 

        foreach ($categoryChildren as $categoryChildId) { 

         /* @var $_childCategory Mage_Catalog_Model_Category */ 
         $_childCategory = $_category = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryChildId); 

         $o .= '<li><a href="'.$_childCategory->getUrl().'">'.$_childCategory->getName().'</a></li>'; 

         // If you wish to display the total number of products for each category, uncomment this line 
         // $o .= '<span class="totalNumberOfProducts">'.$_childCategory->getProductCollection()->count().'</span>'; 

        } 

        $o .= '</ul></li>'; 
       } 

     } 
    } $o .='</ul>'; 


echo $o; 
?> 
</div> 

但頂部的菜單網址是錯誤的所有其他正在顯示正確的,但主要的類別網址是錯誤(第二類)請幫助我,我也必須顯示第三級菜單還當用戶懸停在類別... 實時鏈接http://toolskaart.com/

回答

0

希望下面的代碼將解決您的問題。

Mage::getModel('catalog/category')->load($_category->getId())->getUrl() 

它適用於我。

+3

這是一個完美的例子,不要在Magento中做什麼。在Magento的每次迭代(!)中從數據庫重新加載整個對象將不會很有效。 –

+0

你能舉個例子嗎?如何重寫它。 – Sethu

+0

使用模型集合 - 即立即加載整個集合(使用id過濾),然後迭代對象。它將使用一個查詢而不是'n',使其顯着更快。 –