2

A bit of a simple one, I just want to load a category collection and for it to include disabled categories either on their own or along with the enabled categories.

It seems that the standard collection call has an automatic 'active' filter on it:

Mage::getModel('catalog/category')->getCollection();

If anyone's done this before it would be a great help!

3 Answers 3

3

I had the same problem as you:

I was passing the following into the addIdFilter:

$category->getChildren();

Internally, getChildren() as well as getAllChildren() use Mage_Catalog_Model_Resource_Category::getChildren() which explicitly joins the is_active attribute and returns ids of active children only.

But the resource model also has a getChildrenIds() method which does not add this join. Also it conveniently returns a comma separated list by default.

Solution

Replace

$categoryCollection->addIdFilter(explode(',', $parentCategory->getChildren()));

with

$categoryCollection->addIdFilter(
    $parentCategory->getResource()->getChildrenIds($parentCategory)
);
2

I'd expect the following to give you what you need:

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addFieldToFilter('is_active',array("in"=>array('0', '1')))
    ->addAttributeToSelect('*'); // optional
1
  • No, it brings back active and inactive categories. just tested it, and it works fine. Commented Jun 11, 2014 at 19:00
2

I realised I was being an idiot!

I was passing the following into the addIdFilter:

$category->getChildren();

Instead of:

$category->getAllChildren();

My final code is now this:

$category = Mage::getModel('catalog/category')->load($id);
    $categories = $category->getCollection()
    ->addAttributeToSelect(array('name', 'thumbnail', 'description'))
    ->addIdFilter($category->getAllChildren());
2
  • Seems a little backward to get the collection, and then filter by all the IDs that are available anyway. What if you wanted to use the addIdFilter to filter down certain categories, but required active and inactive categories to be returned? Commented Jun 11, 2014 at 19:02
  • I'm wondering why this worked for you, getAllChildren() calls getChildren() and only adds the id of the category itself. You probably changed something else as well Commented Aug 30, 2016 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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