0

我正在一个网站上显示与当前类别相关的所有子类别的列表。下面的代码可以正常工作,但我想更改子类别列表的排序方式。目前,它按类别 ID 排序。我希望它以 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>';
                    }
                }
            ?>
4

1 回答 1

6

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

$loadCategory->getChildrenCategories()

已编辑

与返回所有类别 ID 的字符串的 getChildren 不同,它返回一个 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>';
    }
}
?> 
于 2013-02-15T16:43:51.463 回答