php - magento - 检索给定类别ID的所有子类别

标签 php magento magento-1.4 magento-1.5

正如标题中所说,我正在尝试通过我的自定义函数来做这些事情:

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/

相关文章:

php - Magento SQLSTATE[42S22]

php - Magento/PHP-修改前端产品搜索框下面的MySQL查询

php - 错误 1064 - MySQL

php - MySQL 条件连接查询(WordPress 数据库)

php - SimpleXML 根节点前缀 php

Magento:如何检索(即)分组产品的最低价格?

magento-1.4 - 为什么从 Magento 1.3 升级后订单中的付款方式信息不可用

magento - Magento 服务暂时不可用?

magento - 在什么情况下 Magento 订单增量 ID 可以有重复?

php - 使用 PHP 中 RootElement 中的命名空间解析 XML