1. 获取指定分类id 下的子分类
// 例如获取 分类id为 13 的子分类信息
<?php $root = Mage::getModel('catalog/category')->load(13); $subCat = explode(',',$root->getChildren()); $collection = $root ->getCollection() ->addAttributeToSelect("*") ->addFieldToFilter("entity_id", array("in", $subCat) ); foreach($collection as $catname){ echo $catname->getName(); echo '<br>'; echo $catname->getUrl(); } ?> echo '<br>'; //echo $catname->getProductCount(); //获取目录下的产品数量 //echo $catname->getChildrenCount(); //获取目录下子目录数量
2. 显示所有分类和子分类
<?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories() ?> <?php if (count($_categories) > 0): ?> <ul> <?php foreach($_categories as $_category): ?> <li> <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"> <?php echo $_category->getName() ?> </a> <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?> <?php $_subcategories = $_category->getChildrenCategories() ?> <?php if (count($_subcategories) > 0): ?> <ul> <?php foreach($_subcategories as $_subcategory): ?> <li> <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> <?php echo $_subcategory->getName() ?> </a> </li> <?php endforeach; ?> </ul> <?php endif; ?> </li> <?php endforeach; ?> </ul> <?php endif; ?>
3.获取所有的一级分类信息
<?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories() ?> <?php if (count($_categories) > 0): ?> <ul> <?php foreach($_categories as $_category): ?> <li> <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"> <?php echo $_category->getName() ?> </a> </li> <?php endforeach; ?> </ul> <?php endif; ?>
4.获取所有的一级分类和当前分类的子分类信息
<?php $_helper = Mage::helper('catalog/category') ?> <?php $_categories = $_helper->getStoreCategories() ?> <?php $currentCategory = Mage::registry('current_category') ?> <?php if (count($_categories) > 0): ?> <ul> <?php foreach($_categories as $_category): ?> <li> <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"> <?php echo $_category->getName() ?> </a> <?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?> <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?> <?php $_subcategories = $_category->getChildrenCategories() ?> <?php if (count($_subcategories) > 0): ?> <ul> <?php foreach($_subcategories as $_subcategory): ?> <li> <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> <?php echo $_subcategory->getName() ?> </a> </li> <?php endforeach; ?> </ul> <?php endif; ?> <?php endif; ?> </li> <?php endforeach; ?> </ul> <?php endif; ?>
<< — 代码整理自网络搜集总结 —>>
此文章通过 python 爬虫创建,原文是自己的csdn 地址: magento 获取分类及其子分类 多种情况举例