码迷,mamicode.com
首页 > 其他好文 > 详细

magento 获取分类及其子分类 多种情况举例

时间:2015-03-20 11:01:38      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:magento

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; ?>

<< ---  代码整理自网络搜集总结 --->>




magento 获取分类及其子分类 多种情况举例

标签:magento

原文地址:http://blog.csdn.net/m0sh1/article/details/44487121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!