2013-08-16 48 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',使其显着更快。 –