我想知道如何按照管理面板中的位置对magento中的类别列表(我在此处遵循本教程http://www.devinrolsen.com/magento-custom-category-listing-block/)进行排序?目前按ID
排序<?php
$cats = Mage::getModel('catalog/category')->load(3)->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>
答案 0 :(得分:22)
你为自己试图处理身份证和东西做了太多的工作。以下内容已按位置排序。
<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
<li>
<a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a>
</li>
<?php endforeach; ?>
</ul>
答案 1 :(得分:5)
如果您想按照adminhtml中创建的位置对类别进行排序,那么由于catalog/category
是Mage_Catalog_Model_Resource_Category_Collection
的实例,因此请在您指定要选择的内容的位置进行查询,过滤和/或排序。
此处的案例是从catalog_category_entity
获取类别,仅选择名称,在ID后过滤,排序 position
上的查询。
<?php
$subcategories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id', $categoryId)
->addAttributeToSort('position', ASC);
?>
答案 2 :(得分:3)
这就是我所做的:
$allCategories = Mage::getModel('catalog/category');
$CategoriesTree = $allCategories->getTreeModel()->load();
$categoriesIds =
$CategoriesTree->getCollection()->addAttributeToSort('position', 'asc')->getAllIds();
检索类别后:
$categoryChildren = array();
if ($categoriesIds) {
foreach ($categoriesIds as $categoryId){
$category = Mage::getModel('catalog/category')->load($categoryId);
$categoryChildren[] = $category;
}
}
然后:
// Sort by position
function comparePosition($a, $b) {
if ($a->position == $b->position)
return 0;
return ($a->position > $b->position) ? 1 : -1;
}
usort($categoryChildren, 'comparePosition');
反转返回(1和-1)显然会改变顺序。 它对我来说很好。 希望它可以帮到某人。
答案 3 :(得分:1)
我强烈建议先在这里http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections,并且知识库中的其他文章是任何magento dev的必读内容。
<?php
$cats = Mage::getModel('catalog/category')->addAttributeToSort('yourfield', 'desc')->getCollection()->getChildren();
$catIds = explode(',',$cats);
?>
答案 4 :(得分:0)
<?php
$model_category = Mage::getModel('catalog/category')->load($_category->getEntityId());
$sub_categories = $model_category->getCollection();
$sub_categories -> addAttributeToSelect('url_key')
-> addAttributeToSelect('name')
-> addAttributeToFilter('is_active',1)
-> addIdFilter($model_category->getChildren())
-> setOrder('position', 'ASC')
-> load();
?>
<?php foreach($sub_categories->getData() as $each_subcat): ?>
<?php $model_subcat = Mage::getModel('catalog/category')->load($each_subcat['entity_id']); ?>
<?php endforeach; ?>