douluhaikao93943 2012-03-21 13:53
浏览 36
已采纳

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

As said in the title, i'm trying to do this stuff through my custom function:

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);
}

but seems that there's something wrong in the stop or in the exit condition. the function retrieve correctly first 16 elements. anybody could help me?

  • 写回答

1条回答 默认 最新

  • dtrhd2850 2012-03-21 14:35
    关注

    I think the class Mage_Catalog_Model_Category already includes the function you are searching. It is called getChildren:

    public function retrieveAllChilds($id = null, $childs = null) {
        $category = Mage::getModel('catalog/category')->load($id);
        return $category->getChildren();
    }
    

    The function getChildren returns children IDs comma-separated, getChildrenCategories returns an array of Mage_Catalog_Model_Category instances.

    If you want to get the children categories recursively, you can use:

    public function retrieveAllChilds($id = null, $childs = null) {
        $category = Mage::getModel('catalog/category')->load($id);
        return $category->getResource()->getChildren($category, true);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题