$allCats = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('parent_id',array('eq' => $parentId))
->addAttributeToSort('position', 'asc');

Currently, this code showing all category including empty category I want to remove empty category from collection


Try this one:

$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToFilter('level', 2)
    ->addAttributeToSelect('is_active', 1)

foreach ($categories as $category) {
    echo $category->getProductCount();
  • 1
    Think $categories->setLoadProductCount(true); is a good idea :) – sv3n Apr 19 '19 at 13:23

You can remove empty categories from category collection

foreach($allCats as $key => $category) {
    if ($category->getProductCount() === 0) {

I hope this will help

  • Not possible to check without loop/outside the loop?, Can directly exclude from the collection ? – Wordpress Developer Apr 17 '19 at 5:36
  • I don't know the way of excluding categories from collection without loop. I think using loop it may be the way to exclude the categories from collection not delete it. – Muhammad Hasham Apr 17 '19 at 5:40

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.