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

发布于 2024-10-03 23:14:20 字数 873 浏览 9 评论 0原文

在 Magento 中,我使用下面的代码创建了一个 phtml 模板文件。我从本教程中得到了这个。我和其他人想知道如何按字母顺序对这个类别列表进行排序。代码的第一行创建一个包含类别 ID 的数组。再往下,我们可以使用 foreach 部分中的 ID 获取类别名称。但要按名称排序,我们需要在 foreach 之前获取数组中的名称,然后按名称排序。如何?

<?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 页面中作为块插入(该部分已经在工作)。

In Magento, I've created a phtml template file with the code below. I got this from this tutorial. Me and others are wondering how to sort this category list alphabetically. The first lines of code create an array with Category IDs. Further down, we can get the Category Name using the ID within the foreach section. But to sort by Name, we need to get the Names in an array before the foreach and then sort by name. How?

<?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>

Note: 319 is the category id of the parent category for which I want to list subcategories. Also, I'm not putting this is a category page template. I'm inserting as a block in a CMS page (that part is already working).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

辞旧 2024-10-10 23:14:20

您可以调用

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

,您将立即对整个集合进行排序,其余的只是对典型 Varien 集合的迭代。这是一个伪样本,我不知道parent_id是否是数据库中的实际字段名称,因此您可以在获得正确的结果之前检查一下。

关于 Magento 中集合的精彩阅读是作者:艾伦·斯托姆

You can call

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

and you'll get the whole bunch sorted right away, rest is just iteration over typical Varien collection. It's a pseudo sample and i don't know if the parent_id is the actual field name in db so you might check that out before you get the right results.

Great read about collections in Magento is written by Alan Storm

我不是你的备胎 2024-10-10 23:14:20

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

<?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 的答案更好,更 Magentic(?)

You could build a list of category names first.

<?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>

I wrote this answer without knowing too much about Magento and just wanting something quickly that worked. Anton's answer is better and more Magentic(?)

爱要勇敢去追 2024-10-10 23:14:20

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

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

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

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

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

希望这也适合你。

Hi I am using magento 1.4.1.1 and this worked to sort the child categories:
use

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

to get the children categories of category with id 319
and edit the file at code/core/Mage/catalog/Model/Resource/Eav/Mysql4/category.php at line 582 under the function getChildrenCategories() to change

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

to

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

hope this works for you too.

白云不回头 2024-10-10 23:14:20

在最新的 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 第 817 行左右

有很多选项。希望它能为您节省一些时间!

There's a much easier way to do this in the latest 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;

The function getChildren() resides at...

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

There are loads of options. Hope it saves you some time!

残花月 2024-10-10 23:14:20

首先备份您的 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>

First backup your topmenu.phtml then replace the following code in your new topmenu.phtml file

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文