In Magento if we are required to load the Categories with their product count,
Then to do so, let see how:
First of all load the current root category for say we have default category with id: 2.
To load the root category we can write:
$rootcategory = Mage::getModel('catalog/category')->getCategories('2');
Then we are required to load the subcategories the this for this write a foreach loop
if(count($rootcategory) > 0 ){
foreach ($rootcategory as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
}
}
But we are required to print the categories with their total no of product the same edit the foreach loop and add total no of product in it as :
if(count($categories) > 0 ){
foreach ($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
echo 'Category name :'.$cat->getName();
$products_count = Mage::getModel('catalog/category')->load($category->getId())->getProductCount();
echo ' Product Count :'.$products_count.'<br/>';
}
}
In the above code in foreach loop first we loaded the category from the model 'catalog/category' with the category id and print it.
Afterwards, we loaded the product collection count from the same model with the magento function getProductCount() and print it.
This way we can get all Categories with their product count.
0 Comment(s)