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!

2

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
  • No, it brings back active and inactive categories. just tested it, and it works fine. – Douglas Radburn Jun 11 '14 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());
  • 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? – Douglas Radburn Jun 11 '14 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 – Fabian Schmengler Aug 30 '16 at 7:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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