magento使用TreeNode方式输出指定查询条件的分类
时间:2016-10-21 14:45来源:未知 作者:好模板 点击:次
magento 使用Category Collection手工构造sql输出制定查询条件的分类,效率低,代码复杂,本例介绍另一种方式 Php代码 ?php //initializemagentoenvironmentfordefaultstore require_once ../../app/Mage.php ; Mage::a
magento使用Category Collection手工构造sql输出制定查询条件的分类,效率低,代码复杂,本例介绍另一种方式
-
<?php
-
-
require_once '../../app/Mage.php';
-
Mage::app('default');
-
-
function getStoreCategories($sorted=false, $asCollection=false, $toLoad=true) {
-
$parent = 6;
-
$category = Mage::getModel('catalog/category');
-
if (!$category->checkId($parent)) {
-
if ($asCollection) {
-
return new Varien_Data_Collection();
-
}
-
return array();
-
}
-
-
$recursionLevel = max(0, 0);
-
-
$tree = $category->getTreeModel();
-
$nodes = $tree->loadNode($parent)
-
->loadChildren($recursionLevel)
-
->getChildren();
-
-
$tree->addCollectionData(null, $sorted, $parent, $toLoad, true);
-
-
if ($asCollection) {
-
return $tree->getCollection();
-
} else {
-
return $nodes;
-
}
-
}
-
-
function getCategory($categoryNode){
-
$category = Mage::getModel('catalog/category');
-
$category->setData($categoryNode->getData());
-
return $category;
-
}
-
-
-
-
header("content-Type: text/html ; charset=utf-8");
-
?>
-
<!-- 输出两层Category -->
-
<?php foreach (getStoreCategories() as $_categoryNode) { ?>
-
<div class="catwrapper2">
-
<div class="subcat2" id="subcat1">
-
<?php $category =getCategory($_categoryNode); ?>
-
<h2 class="maincat inline" id="cat1">
-
<a href="<?php echo $category->getUrl(); ?>"><?php echo $category->getName(); ?>(<?php echo $category->getProductCount(); ?>)</a>
-
</h2>
-
<?php $hasChildren = $_categoryNode->hasChildren(); ?>
-
<?php if($hasChildren){
-
foreach ($_categoryNode->getChildren() as $subCategoryNode){
-
$subCategory =getCategory($subCategoryNode);
-
?>
-
<a href="<?php echo $subCategory->getUrl(); ?>"><?php echo $subCategory->getName(); ?></a>
-
<?php
-
}
-
}
-
?>
-
</div>
-
</div>
-
<?php } ?>
代码范例中的getCategory方法是获得Node对应的Cateogry模型实例,目的仅仅是为了调用它的getUrl方法.
下面是我机器上的输出结果:
(责任编辑:好模板) |
------分隔线----------------------------