Magento获取子分类及产品数量

很多Magento的项目中,客户需求将每个当前分类下的每个子分类以及该分类下的产品数量全部显示出来,类似于Category (108)的形式。如下所示想实现这种效果,就必须要知道如何获取当前分类的子分类,并了解Product Collection类中的count()方法。该方法用于获取任意形式下对于Product Collection进行筛选后的产品数量。

// 获取当前分类模型
$currCat = Mage::registry('current_category');
 
//获取当前分类的所有子分类的模型集合
$collection = Mage::getModel('catalog/category')->getCategories($currCat->getEntityId());
 
//循环子分类的模型集合
foreach($collection as $cat) {
    if($cat->getIsActive()) {
        $category = Mage::getModel('catalog/category')->load($cat->getEntityId());
 
        //获取子分类的产品Collection,并通过count()方法,获取该子分类的产品数量
        $prodCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category);
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($prodCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($prodCollection);
 
        $html .= '<a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a> (<?php echo $prodCollection->count() ?>)<br/>';
    }
 

你可能感兴趣的:(agent)