Tuesday 12 May 2015

Magento – Get current categories of subcategory with product count in magento

<?php
$currCat = Mage::registry('current_category');

/**
 * get sub categories of current category
 */
$collection = Mage::getModel('catalog/category')->getCategories($currCat->getEntityId());

/**
 * looping through sub categories
 * only showing active sub categories ($cat->getIsActive())
 */
foreach ($collection as $cat) {
    if ($cat->getIsActive()) {
        $category = Mage::getModel('catalog/category')->load($cat->getEntityId());

        /**
         * getting product collection for a particular category
         * applying status and visibility filter to the product collection
         * i.e. only fetching visible and enabled products
         */
        $prodCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($category);
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($prodCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($prodCollection);
        ?>

        <a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a> (<?php echo $prodCollection->count() ?>)<br/>

        <?php
    }
}

No comments:

Post a Comment