【问题标题】:Get magento category tree as an assoc array for specific store获取magento类别树作为特定商店的关联数组
【发布时间】:2016-01-11 13:05:41
【问题描述】:

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

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

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

这可能吗?

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

http://devdocs.magento.com/guides/m1x/api/soap/catalog/catalogCategory/catalog_category.tree.html

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

我已经尝试使用这种方法http://magentotutorial.in/how-to-create-categories-tree-structure-in-magento-programmatically/ 作为我的起点,但我得到的只是<ul></ul>

【问题讨论】:

    标签: php magento


    【解决方案1】:
    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;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多