I am having products that belongs to multiple categories.

eg :

Main category 1 
 sub category 1
 sub category 2

Main category 2
 sub category 1
 sub category 2


I am loading product collection from Main category 2 -> sub category 1 In this collection some of my products belongs to Main category 1 -> sub category 1 OR sub category 2 OR any other Main category (n)

I know categories comes as layered nav out of box in Magento. But here it is not showing up.

Is it because I am already Loading a sub category collection? And by any means can i get the categories of the current loaded collection?

  • So if all the categories are anchors as you described (in a deleted comment so stating it here) and products are added to the subs it should work. Did you refresh the indexes to be sure? Dec 12, 2013 at 10:37
  • @SanderMangel: Yes indexes are refreshed.But there is a gomage extension that is installed for navigation. But it somehow not appearing, but if i give cat=catId then then filtering is happening
    – zamil
    Dec 12, 2013 at 10:49

2 Answers 2


For a custom category filter like that you need to rewrite the class Mage_Catalog_Model_Layer_Filter_Category, specifically the method _getItemsData() that determines which values should be shown in the filter.

The relevant lines there are:

$categories = $categoty->getChildrenCategories();


(yes, it's categoty...)

You see that only children of the current category are taken into account, then it will be checked how many products of the current product collection are in each of these.

You need to replace $categories = $categoty->getChildrenCategories(); but be careful, the whole collection gets loaded, so it's a bad idea to use all categories:

$categories = Mage::getResourceModel('catalog/category_collection');

(except if you only have a hand full of categories, then you can stop here)

So let's prepare this collection to only load the categories we need:

$resource = $categories->getResource();
$adapter = $resource->getReadConnection();
$productCategoryTable = $resource->getTable('catalog/category_product');

$select = $adapter->select()
    ->from($productCategoryTable, 'category_id')
    ->join(array('category' => $categoryTable), 'product_id=
    ->where('product_id IN (?)', $productCollection->getAllIds());
$allCategoryIds = $adapter->fetchCol($select);


There is still optimization potential, using joins in a single query instead of loading the product ids first, then the category ids, then adding the filter but this is not possible anymore with the Magento ORM alone, you'll need to manipulate the underlying Zend_Db_Select directly which will get a lot more complex. I'd try if this solution is sufficient and optimize when needed.


if question: "And by any means can i get the categories of the current loaded collection?" the short answer is of course "Yes".

More detailed answer is
1. get all product IDs from the collection;
2. find corresponding categories id in the table catalog_product_category;
3. load found categories by their IDs using the default category collection;

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.