正如标题中所说,我正在尝试通过我的自定义函数来做这些事情:
public function retrieveAllChilds($id = null, $childs = null){
$childIdsArray = is_null($childs) ? array() : $childs;
$category = is_null($id) ? $this->getCurrentCategory() : $this->getCategoryFromId($id);
if (count($this->getChildrenCategories($id)) > 0) {
$c = count($this->getChildrenCategories($id));
$tmp_array = array();
foreach ($this->getChildrenCategories($id) as $category) {
array_push($tmp_array, $category->getId());
}
$childIdsArray = array_merge($childIdsArray, $tmp_array);
foreach ($this->getChildrenCategories($id) as $category){
$this->retrieveAllChilds($category->getId(), $childIdsArray);
}
}
else{
return array_unique($childIdsArray);
}
return array_unique($childIdsArray);
}
但似乎停止或退出条件有问题。该函数正确检索前 16 个元素。有人可以帮助我吗?
最佳答案
我认为类(class)Mage_Catalog_Model_Category
已经包含您正在搜索的功能。它被称为getChildren
:
public function retrieveAllChilds($id = null, $childs = null) {
$category = Mage::getModel('catalog/category')->load($id);
return $category->getChildren();
}
函数getChildren
返回以逗号分隔的子 ID,getChildrenCategories
返回 Mage_Catalog_Model_Category
实例数组。
如果你想递归地获取子类别,你可以使用:
public function retrieveAllChilds($id = null, $childs = null) {
$category = Mage::getModel('catalog/category')->load($id);
return $category->getResource()->getChildren($category, true);
}
关于php - magento - 检索给定类别ID的所有子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9806025/