2013-02-15 101 views
0

我正在一个网站上显示与当前类别关联的所有子类别的列表。下面的代码正常工作,但我想改变子类别列表的排序方式。目前,它按类别ID排序。我希望它以Magento用户将类别放入管理员的任何顺序显示出来(他们可以通过拖放来更改类别顺序)。感谢任何帮助!更改Magento子类别的排序顺序

   <?php 
       $currentCat = Mage::registry('current_category'); 

       if ($currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId()) 
       { 
        // current category is a toplevel category 
        $loadCategory = $currentCat; 
       } 
       else 
       { 
        // current category is a sub-(or subsub-, etc...)category of a toplevel category 
        // load the parent category of the current category 
        $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId()); 
       } 
       $subCategories = explode(',', $loadCategory->getChildren()); 

       foreach ($subCategories as $subCategoryId) 
       { 
        $cat = Mage::getModel('catalog/category')->load($subCategoryId); 

        if($cat->getIsActive()) 
        { 
         echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a>'; 
        } 
       } 
      ?> 

回答

6

尝试调用getChildrenCategories这将考虑到每个类别的位置:EDITED

不同于返回所有类别ID的字符串的getChildren

$loadCategory->getChildrenCategories() 

返回Mage_Catalog_Model_Category的阵列所以你的代码将需要改变,以考虑到这一点。

从上面的代码片段中,以下更改应该可以工作。请注意对getChildrenCategories()的调用以及foreach循环中的更改,因为每个项目都应该是一个类别对象。

<?php 
$currentCat = Mage::registry('current_category'); 

if ($currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId()) 
{ 
    // current category is a toplevel category 
    $loadCategory = $currentCat; 
} 
else 
{ 
    // current category is a sub-(or subsub-, etc...)category of a toplevel category 
    // load the parent category of the current category 
    $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId()); 
} 
$subCategories = $loadCategory->getChildrenCategories(); 

foreach ($subCategories as $subCategory) 
{ 
    if($subCategory->getIsActive()) 
    { 
     echo '<a href="'.$subCategory->getURL().'">'.$subCategory->getName().'</a>'; 
    } 
} 
?> 
+0

函数是递归的,因为它加载由Children()过滤的集合。请注意,第一个函数getChildren返回一个所有儿童id的字符串,getChildrenCategories返回一个Mage_Catalog_Model_Category的数组。 – dagfr 2013-02-15 19:22:16

+0

感谢您的反馈,我用这些信息更新了我的答案。 – dmanners 2013-02-15 20:07:51

+0

不客气。请注意,主要变化是:不再需要爆炸()和无负载()。更简洁,更简单的代码。 – dagfr 2013-02-15 20:31:32