magento 得到树形结构的分类列表
2024-08-25 22:49:58
<?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(' ', $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;
}
}
最新文章
- Linux下,使用Git管理 dotfiles(配置文件)
- oracle 查询数据库表空间大小和剩余空间
- (5)分布式下的爬虫Scrapy应该如何做-windows下的redis的安装与配置
- 虚拟机Linux----Ubuntu1204----设置固定Ip
- java 21-13 合并
- Java 线程并发策略
- hdu 5755(高斯消元——模线性方程组模板)
- maven项目转eclipse工程的命令:eclipse.bat
- mysql关联更新
- Android创建启动画面
- oracle从备份归档日志的方法集中回收
- 使用 pprof 和 Flame-Graph 调试 Golang 应用
- Java之CyclicBarrier使用,任务等待
- ES6中的解构赋值
- [HNOI2018]排列
- Redis一次数据丢失
- Jenkins插件开发(二)-- HelloWorld
- Linus&#39; Law
- 认识单点登录cas
- 03、同事分享课程的笔记 —《Android应用低功耗设计》