博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
magento 得到树形结构的分类列表
阅读量:7297 次
发布时间:2019-06-30

本文共 2039 字,大约阅读时间需要 6 分钟。

<?php
?>
<?php
 
class Lehui_AllCategoryList_Block_List extends Mage_Core_Block_Template
{
    
    protected $_store;
    protected $_baseUrl;
    
    protected function _construct(){
        parent::_construct();
        $this->_init();
    }
    
    protected function _init(){
        $this->_store=Mage::app()->getStore();
         $this->_baseUrl=Mage::getBaseUrl();
    }
    
    
    public function getAllCategory(){
        
        $parentId = 1;
        
        $tree = Mage::getResourceSingleton('catalog/category_tree')->load();
        $root = $tree->getNodeById($parentId);
        if($root && $root->getId() == 1) {
            $root->setName(Mage::helper('catalog')->__('Root'));
        }
        foreach(get_class_methods($tree) as $item){
           // echo $item,"</br>";
        }
        $collection = Mage::getModel('catalog/category')->getCollection()
                        ->addAttributeToSelect('name')
                        ->addAttributeToSelect('url_path')
                        ->addAttributeToSelect('path')
                        ->addAttributeToSelect('is_active')
                        ->addIsActiveFilter();
        //echo $collection->getSelectSql();//->addPathFilter('1\\/'.$this->_store->getRootCategoryId().'\\/')
        foreach(get_class_methods($collection) as $item){
            //echo $item,"</br>";
        }
        $tree->addCollectionData($collection, true);
        $root=$this->_nodeToArray($root);
        $this->_print_tree($root['children'],0);
    }
    
    
    protected function _print_tree($tree,$level){
        $level++;
        foreach($tree as $item){
            if($level>1&preg_match('/1\\/'.$this->_store->getRootCategoryId().'\\//',$item['path'])){
                echo str_repeat('&nbsp;&nbsp;', $level).'<a href="'.$item['url'].'">'.$item['name']."</a><br>";
            }
            $this->_print_tree($item['children'],$level); 
        }
    }
    
    
    protected function _nodeToArray(Varien_Data_Tree_Node $node){
        $result = array();
        $result['category_id'] = $node->getId();
        $result['parent_id'] = $node->getParentId();
        $result['name'] = $node->getName();
        $result['is_active'] = $node->getIsActive();
        $result['position'] = $node->getPosition();
        $result['level'] = $node->getLevel();
        $result['url']=$this->_baseUrl.$node->getData('url_path');
        $result['path']=$node->getData('path');
        $result['children'] = array();
        
        foreach ($node->getChildren() as $child) {
            $result['children'][] = $this->_nodeToArray($child);
        }
        
        return $result;
    }
    
    
    
}

转载地址:http://ksfnm.baihongyu.com/

你可能感兴趣的文章
计算机专业学习浅谈
查看>>
.NET 进程和线程
查看>>
设计数据库的三范式
查看>>
【c学习-6】
查看>>
自测题的整理(持续更新)
查看>>
DAMS2019中国数据智能管理峰会将于7月在上海召开!
查看>>
[原创]TimeQuest约束外设之诡异的Create Generated Clocks用法
查看>>
Unity UGUI —— 无限循环List(转载)
查看>>
【总结整理】《人人都是产品经理》---读后感
查看>>
第23件事 评估产品或项目是否靠谱的7个标准
查看>>
MySQL的优化与执行
查看>>
04-人员增删改查
查看>>
Python之自动单元测试之一(unittest使用实例)
查看>>
ORA-04031:oracle无法分配共享内存
查看>>
Mysql SQL Mode详解
查看>>
理解WebKit和Chromium: Chromium for Android
查看>>
Floyd算法 笔记 C/C++
查看>>
Android中再按一次退出实现
查看>>
基于Unity3D 的Vuforia SDK开发基础教程
查看>>
用户,群组和权限 二
查看>>