Monday 21 April 2014

How to get category and product count in magento

app/design/frontend/base/default/template/catalog/navigation/left.phtml
below 

<?php
    function GetProductCount($catid='',$productcount=0)
    {
        if($catid != '')
        {
            $actualCategory = Mage::getModel('catalog/category')->load($catid);
            $subCategories = explode(',', $actualCategory->getChildren());
            $productcount += $actualCategory->getProductCount();
            foreach ( $subCategories as $subCategoryId )
             {
                if($subCategoryId != '')
                {
                    $actualCategory1 = Mage::getModel('catalog/category')->load($subCategoryId);
                    $productcount = GetProductCount($subCategoryId,$productcount);
                }
            }
            return $productcount;
        }
        else {
            return $productcount;
        }
    }
if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<div class="block block-layered-nav">
    <div class="block-title">
        <strong><span><?php echo $this->__('Browse By') ?></span></strong>
    </div>
    <div class="block-content">
        <dl id="narrow-by-list2">
            <dt><?php echo $this->__('Category') ?></dt>
            <dd>
                <ol>
                <?php foreach ($_categories as $_category): ?>
                    <?php if($_category->getIsActive()):
                        $productCount = $_category->getProductCount();
                    ?>
                    <li><?php $productCounts = GetProductCount($_category->getId());
                    ?>
                        <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $productCounts; ?>)
                    </li>
                    <?php endif; ?>
                <?php endforeach ?>
                </ol>
            </dd>
        </dl>
        <script type="text/javascript">decorateDataList('narrow-by-list2')</script>
    </div>
</div>
<?php endif; ?>

No comments:

Post a Comment