magento-在给定类别ID的情况下检索所有子类别 [英] magento - retrieve all children categories given a category id

查看:49
本文介绍了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);
}

但是在停止或退出条件下似乎出了点问题.该函数正确检索前16个元素.有人可以帮助我吗?

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?

推荐答案

我认为该类 已包含您要搜索的功能.它称为 getChildren :

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

函数 getChildren 返回以逗号分隔的子ID, getChildrenCategories 返回Mage_Catalog_Model_Category实例的数组.

如果要递归获取子类别,则可以使用:

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

这篇关于magento-在给定类别ID的情况下检索所有子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆