如何在Magento中按字母顺序对类别列表数组进行排序

时间:2010-11-25 05:03:03

标签: php magento

在Magento中,我使用下面的代码创建了一个phtml模板文件。我得到了这个from this tutorial。我和其他人想知道如何按字母顺序对此类别列表进行排序。第一行代码创建一个带有Category ID的数组。再往下,我们可以使用foreach部分中的ID获取Category Name。但是要按名称排序,我们需要在foreach之前获取一个数组中的Names,然后按名称排序。怎么样?

<?php
$cats = Mage::getModel('catalog/category')->load(319)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
    <li>
        <?php
            $category = Mage::getModel('catalog/category')->load($catId);
            echo '<a href="' . $category->getUrl() . '">';
            echo $category->getName() . '</a>';
        ?>
    </li>
<?php endforeach; ?>
</ul>

注意:319是我要列出子类别的父类别的类别ID。另外,我不是说这是一个类别页面模板。我在CMS页面中插入一个块(该部分已经在工作)。

5 个答案:

答案 0 :(得分:21)

你可以打电话

Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('parent_id', '319')->addAttributeToSort('name', 'ASC');

你会立即整理整个束,休息只是典型的Varien系列的迭代。这是一个伪示例,我不知道parent_id是否是db中的实际字段名称,因此您可以在获得正确结果之前检查它。

Great read about collections in Magento is written by Alan Storm

答案 1 :(得分:10)

您可以先建立一个类别名称列表。

<?php
$cats = Mage::getModel('catalog/category')->load(319)->getChildren();
$catIds = explode(',',$cats);

$categories = array();
foreach($catIds as $catId) {
       $category = Mage::getModel('catalog/category')->load($catId); 
       $categories[$category->getName()] = $category->getUrl();
}

ksort($categories, SORT_STRING);
?>

<ul>
<?php foreach($categories as $name => $url): ?>
    <li>
    <a href="<?php echo $url; ?>"><?php echo $name; ?></a>
    </li>
<?php endforeach; ?>
</ul>

我在不太了解Magento的情况下编写了这个答案,只想快速找到有用的东西。 Anton's answer is better and more Magentic(?)

答案 2 :(得分:1)

您好我使用的是magento 1.4.1.1,这可以对子类别进行排序: 使用

$cats = Mage::getModel('catalog/category')->load(319)->getChildrenCategories();

获取ID为319的子类别 并在函数code/core/Mage/catalog/Model/Resource/Eav/Mysql4/category.php下的第582行getChildrenCategories()处编辑文件以更改

->setOrder('position','ASC'); 

->setOrder('name','ASC);

希望这也适合你。

答案 3 :(得分:1)

在最新的Magento(CE 1.7.0.2)+

中有一种更简单的方法
$children = Mage::getModel('catalog/category')->getCategories(319, 1, true, true);

// iterate through the results
foreach ($children as $category):
    echo '<option value="' . $category->getUrl() . '">' . $category->getName() . '</option>';
endforeach;

函数getChildren()位于......

app/code/core/Mage/Catalog/Model/Category.php around line 817

有很多选择。希望它能节省你一些时间!

答案 4 :(得分:1)

首先备份你的topmenu.phtml然后在你的新topmenu.phtml文件中替换以下代码

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php

function array_sort($array, $on, $order=SORT_ASC){
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
            asort($sortable_array);
            break;
            case SORT_DESC:
            arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

?>
<?php
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
?>
<div class="nav-container">
    <ul id="nav">
    <?php $_helper = Mage::helper('catalog/category') ?>
    <?php $_categories = $_helper->getStoreCategories() ?>
    <?php $currentCategory = Mage::registry('current_category') ?>
    <?php if (count($_categories) > 0){ ?>
        <?php foreach($_categories as $_category){ ?>
            <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
            <li><a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><span><?php echo $_category->getName(); ?></span></a>
            <?php $catList = array();?>
            <?php $_subcategories = $_category->getChildrenCategories() ?>
            <?php foreach($_subcategories as $_subCategory){ ?>
                <?php $catList[] = array('name' => $_subCategory->getName(), 'url' => $_subCategory->getUrl(), 'id' => $_subCategory->getId());?>
            <?php } ?>
            <?php $catList = array_sort($catList, 'name', SORT_ASC);?>
            <ul>
            <?php if (count($catList) > 0){ ?>
                <?php $subcat=0?>
                <?php foreach($catList as $_subCategory){ ?>
                    <li><a href="<?php echo $_subCategory['url'] ?>"><span><?php echo $_subCategory['name'] ?></span></a>
                    <?php $subCatList = array();?>
                    <?php $_subSubCat = Mage::getModel('catalog/category')->load($_subCategory['id']);
                    $_subSubCategories = $_subSubCat->getChildrenCategories();?>
                    <?php foreach($_subSubCategories as $_subSubCategory){ ?>
                        <?php $subCatList[] = array('name' => $_subSubCategory['name'], 'url' => $_subSubCategory['url']);?>
                    <?php } ?>
                    <?php $subCatList = array_sort($subCatList, 'name', SORT_ASC);?>
                    <?php if (count($subCatList) > 0){ ?>
                        <ul>
                            <?php foreach($subCatList as $_subSubCat){ ?>
                                <li><a href="<?php echo $_subSubCat['url'] ?>"><span><?php echo $_subSubCat['name'] ?></span></a>
                            <?php } ?>
                            </li>
                        </ul>
                    <?php } ?>
                    </li>
                <?php } ?>

                <?php } ?>
            </ul>
            </li>
        <?php } ?>
    <?php } ?>
    </ul>
</div>