0

Currently I am making a custom RWD theme, with the Madison Island sample data. The category 'Women' contains 51 products in total. These products are divided in several different subcategories, for example 'Tops & Blouses'.

I looked around the internet for a piece of code that will help me display the amount of product within a category, and all answers came down to this code:

$categoryId = '4';
$products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
echo $products_count;

Where 4 is the category ID of the category 'Women'. The problem is, is that this snippet echoes '3'. But I don't have 3 product, I have 51. Why is this?

1

Use below code

/** 
 * If you are in any category page then load its parent category
 * Else load root category; e.g root category has ID = 3  
 *
 **/
if (is_object(Mage::registry('current_category'))) {
    $parentCategory =  Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId())->getParentCategory();
} else {    
    $rootCategoryId = 3;
    $parentCategory =  Mage::getModel('catalog/category')->load($rootCategoryId);
}

<!--more-->

//$_categories = $parentCategory->getChildren(); 

$_categories = $parentCategory->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');

//$layer = Mage::getSingleton('catalog/layer');
//$layer->prepareProductCollection($productCollection);

$productCollection->addCountToCategories($_categories);

// Displaying active categories with their product count
foreach ($_categories as $_category): 
    if($_category->getIsActive()): 
?>
        <a href="<?php echo $_category->getUrl() ?>">
            <?php echo Mage::helper('core')->htmlEscape($_category->getName()) ?>
        </a> 
        <?php echo '('.$_category->getProductCount().')'; ?>
        <br />
<?php        
    endif; 
endforeach; 
?>

Hope it helps. Thanks.

  • worked like a charm! Only thing is, that I have made a little function of it. I will also put that in as an answer, but without this I could not have done it! – Maarten Wolfsen Sep 2 '16 at 9:12
2

Go to admin and edit this category (id=4). Now go to Category Products tab and check how many products are there. I think there will be 3 products. The total you are looking for is the sum of products of all it's sub categories.

Use this code instead:

$categoryId = '4';
echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->getSize();    

//Using count(). But getSize() is much faster
//echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->count();
  • This category itself contains 3 products indeed, and 4 sub categories. These subcategories do contain products. I need the sum of all the products that are in the subcategories of the category 'women'. – Maarten Wolfsen Sep 2 '16 at 8:38
  • check updated answer – Anil Suthar Sep 2 '16 at 8:39
0

Use Below code for countiong prouduct

$categoryId = '4';
$collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
echo $colletion->getSize();  

getSize() gives you actually size of collection.

  • did not work for some reason – Maarten Wolfsen Sep 2 '16 at 9:11
  • same problem ? have you got 3 instead of 51? – Murtuza Zabuawala Sep 2 '16 at 9:17
  • No, it resulted in probably an error. The rest of the page wouldn't load – Maarten Wolfsen Sep 2 '16 at 9:19
0

From the previous accepted answer, I have created this function, which will return the amount of products in the category containing subcategories:

function get_cat_count($int){
    $rootCategoryId = $int;
    $parentCategory =  Mage::getModel('catalog/category')->load($rootCategoryId);
    $_categories = $parentCategory->getChildrenCategories();
    $productCollection = Mage::getResourceModel('catalog/product_collection');
    $productCollection->addCountToCategories($_categories);

    // counting amount of products;
    $cat_count = 0;
    foreach ($_categories as $_category): 
        if($_category->getIsActive()): 
            $cat_count += $_category->getProductCount();
        endif; 
    endforeach; 

    echo $cat_count; 
}
0

It is very simple and it worked well for me its just one line of code

<?php echo Mage::registry('current_category')->getProductCount();?>

It will display the count of the products of current category

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.