2
$allCats = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active','1')
->addAttributeToFilter('include_in_menu','1')
->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

2

Try this one:

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

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

You can remove empty categories from category collection

foreach($allCats as $key => $category) {
    if ($category->getProductCount() === 0) {
        $allCats->removeItemByKey($category->getId());
    }
}

I hope this will help

  • Not possible to check without loop/outside the loop?, Can directly exclude from the collection ? – Wordpress Developer Apr 17 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 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.