php 如何在 Magento 中按字母顺序对类别列表数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4273898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort a category list array alphabetically in Magento
提问by Joe Fletcher
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?
在 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>
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).
注意:319 是我要列出子类别的父类别的类别 ID。另外,我没有把这是一个类别页面模板。我在 CMS 页面中作为一个块插入(该部分已经在工作)。
回答by Anton S
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.
并且您将立即对整组进行排序,其余的只是对典型 Varien 集合的迭代。这是一个伪示例,我不知道 parent_id 是否是 db 中的实际字段名称,因此您可以在获得正确结果之前检查它。
Great read about collections in Magento is written by Alan Storm
回答by alex
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(?)
我在不了解 Magento 的情况下写了这个答案,只是想要一些快速有效的东西。安东的答案更好,更神奇(?)
回答by bhab
Hi I am using magento 1.4.1.1 and this worked to sort the child categories: use
嗨,我正在使用 magento 1.4.1.1,这有助于对子类别进行排序:使用
$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
获取 id 为 319 的类别的子类别,并在要更改code/core/Mage/catalog/Model/Resource/Eav/Mysql4/category.php
的函数下编辑第582 行的文件getChildrenCategories()
->setOrder('position','ASC');
to
到
->setOrder('name','ASC);
hope this works for you too.
希望这也适用于您。
回答by Jongosi
There's a much easier way to do this in the latest Magento (CE 1.7.0.2)+
在最新的 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...
该函数getChildren()
驻留在...
app/code/core/Mage/Catalog/Model/Category.php around line 817
app/code/core/Mage/Catalog/Model/Category.php around line 817
There are loads of options. Hope it saves you some time!
有很多选择。希望它能为您节省一些时间!
回答by user2753425
First backup your topmenu.phtml then replace the following code in your new topmenu.phtml file
首先备份您的 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>