我可以使用以下代码获取特定类别的子类别列表:
public function displaycats($data, $begin,$end, $catid){
$currentCat = Mage::getModel('catalog/category')->load($data[0]);
$children = explode(",",$currentCat->getChildren());
foreach ($children as $child) {
$cot++;
$subCat = Mage::getModel('catalog/category')->load($child);
if ($cot >= $begin && $cot <= $end){
echo '<a href="'.$subCat->getUrl().'?stockable=786">'.$subCat->getName()."</a><br>";
}
}
return;
}
Run Code Online (Sandbox Code Playgroud)
问题是列表的排序与Magento在管理区域中排序的类别不同.谁能帮我吗?
我喜欢@Joe Constant的答案,但是当我尝试它时,我很难获得第一级子类别,即使$recursionLevel设置为0.作为一种解决方法,我编写了这个函数(将它放在(本地版本)你需要排序的子集合的块中):
public function getChildrenCollection($parentId=false, $sort='ASC', $attribute='position')
{
if (empty($parentId) || !is_numeric($parentId)) return false;
$childrenArray = explode(',',Mage::getModel('catalog/category')->load($parentId)->getChildren());
// remove parent id from array in case it gets returned
if ($key = array_search($parentId, $childrenArray)) {
unset($childrenArray[$key]);
}
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $childrenArray))
->addAttributeToSelect('*');
if (!empty($sort)) {
return $collection->setOrder($attribute, $sort);
}
return $collection;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以遍历集合并做你需要的任何事情,如下所示:
foreach (getChildrenCollection($parentCategoryId) as $child) {
Zend_Debug::dump($child->getData());
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
8121 次 |
| 最近记录: |