我有一些代码可以调用主类别的子类别,我需要能够更改网站前端子类别的排序顺序。
我已经尝试将属性添加到排序标记中,但这没有任何作用。有人能帮我指明正确的方向吗。非常感谢:
->addAttributeToSort(’position’, ‘asc’)
这对订单没有任何影响。我使用的代码如下:
<?php
//get the current category
$_cat = new Mage_Catalog_Block_Navigation();
$currentCat = $_cat->getCurrentCategory();
//get the children of the current category
$subCats = Mage::getModel('catalog/category')->load($currentCat->getId())->getChildren();
//get sub category ids
$subCatIds = explode(',',$subCats);
?>
<?php if (count($subCatIds) > 1): ?>
<?php foreach($subCatIds as $subCatId): ?>
<?php $subCat = Mage::getModel('catalog/category')->load($subCatId); ?>
<?php if($subCat->getIsActive()): ?>
@Jason Millward请不要对每个对象都调用load()。它将在不久的将来影响现场性能;)
我为你树立了一个榜样。
$currentCategory = Mage::getModel('catalog/category')->load(3);
$collection = $currentCategory->getCollection();
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->addAttributeToFilter('is_active', 1)
->addIdFilter($currentCategory->getChildren())
->setOrder('position', Varien_Db_Select::SQL_ASC)
->load();
类别实体已经具有由管理员管理的职位属性
只需使用它来订购类别。
你可以试试这个:
您可以在$subCats 之后编写代码
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $subCats))
->addAttributeToSelect('entity_id');
if($collection)
{
$subCatIds = $collection->setOrder('position', 'asc');
}
完成foreach
$subCatId之后,您可以使用id
。希望这会有所帮助。。。
如果您想加载按位置排序的特定类别的子类别。请尝试这个:
/** @var Mage_Catalog_Model_Resource_Category_Flat_Collection $storeCategories */
$storeCategories = Mage::getModel('catalog/category')->getCategories(
$currentCategory->getId(), 0, false, true, false
);
$storeCategories->unshiftOrder('position', Varien_Db_Select::SQL_ASC);
通知我们必须使用unshiftOrder而不是setOrder参考了顶部菜单,看看它是如何工作的。希望这能帮助到别人!