将magento类别树作为特定商店的关联数组

时间:2016-01-10 03:21:06

标签: php magento

在我的magento中,我有以下类别树结构(注意:只扩展了几个类别):

enter image description here

我想生成一个如下所示的数组:

$categories = [
    'Default Category' => [
        'HAIR' => [
            'Hair Colouring' => [
                'Permanent Hair Colour',
                // ...etc
            ],
            'Styling' => [
                'Gel / Wax / Paste / Po',
                // ...etc
            ]
        ],
        // ... etc
    ]
];

这可能吗?

在研究这个问题时,我发现有一种magento soap api方法可能会返回我所追求的内容:

enter image description here

是否可以在使用app/Mage.php的外部php脚本中使用此soap api方法(不使用soap api客户端)?

我尝试使用此方法http://devdocs.magento.com/guides/m1x/api/soap/catalog/catalogCategory/catalog_category.tree.html作为我的起点,但我要回来的只是<ul></ul>

1 个答案:

答案 0 :(得分:0)

public function megamenu(){

        $children = Mage::getModel('catalog/category')->getCategories(2);
        if(count($children) > 0){
            $i=0;
            foreach ($children as $category) {
                //echo $category->getName();
                if($category->getName()){
                $id = $category->getId();
                $data[$i]['id'] = $category->getId();
                $data[$i]['name'] = $category->getName();
                //$data[$i]['subcategory'] = array();
                $data[$i]['subcategory'] = $this->mycategory($id);
                $i++;
                }
            }
        }
        //echo "<pre>";print_r($data);
        return $data;
    }
    public function mycategory($id){
        $children = Mage::getModel('catalog/category')->getCategories($id);
        if(count($children) > 0){
            $i=0;
            foreach ($children as $category) {
                //echo $category->getName();
                $id = $category->getId();
                $sub[$i]['id'] = $category->getId();
                $sub[$i]['name'] = $category->getName();
                $sub[$i]['subcategory'] = $this->mycategory($id);
                $i++;
            }
        }
        return $sub;

    }