I'm attempting to filter a product collection by two different category_ids. The solution i've come up involves generating two seperate product collections each filtered from the original by one category_id then merging them together. How does one merge product collections in this way? Let's take a look:

function categoryProductFilter($collection, $categoryIds){
        $categoryCollectionTwo = $collection->addAttributeToFilter(array(array('attribute' => 'category_id', 'finset' => 773)));
        $categoryCollectionOne = $collection->addAttributeToFilter(array(array('attribute' => 'category_id','finset' => 1550)));
function merg($one,$two){??}

Is there an easier way to filter a product collection by two categories simultaneously? I've only managed to achieve mono-category filtering.

For reference:

$collection->addCategoryFilter(Mage::getModel('catalog/category')->load($categoryId), true);

Appears to filter the collection correctly as well as using some of the above methodology. It's almost absurd that the above cannot be used twice on the same collection to achieve appropriate filtering.

  • This has been done before: magento.stackexchange.com/a/7098/12662 Feb 27, 2015 at 15:19
  • @Woolfie and yet it is still unanswered "This code doesn't work when there are two or more categories to filter and any product is assigned to multiple of these categories. Returns error as Item (Mage_Catalog_Model_Product) with the same id "xx" already exist." I ONLY want the products that below to all the filtered categories. NOT simply a merger of two categories. Feb 27, 2015 at 15:36

2 Answers 2


You need to make ONE collection, not 'merge two collections'.

With category object you can call getChildren to retrieve every child in a comma separated list (an array, not objects of sub-categories). Iterate as required.

You can convert this list of ids to an array, merge with another similarly derived array and implode the array back to the comma separated list of category IDs.

With this list of ids you can then run your collection, with the categories (that were only ever loaded as ids) put into the query with:

->addAttributeToFilter('category_id', array('in' => array('finset' => $cat_ids)))
  • Just as the answer you attempted to link me to your methodology also returns the same error " Item (Mage_Catalog_Model_Product) with the same id "xx" already exist.". Getting an array of categoryIds is a non-issue. Feb 27, 2015 at 16:20
  • With $collection->getSelect()->group('e.entity_id') it will work, this removes the duplicate results from products that are in both categories. Mar 2, 2015 at 8:14
  • @fschmengler Yes, but the thing is, this simply merges both categories. I only want the items that are within both categories. Not one and/or the other. Mar 2, 2015 at 15:00
  • php.net/manual/en/function.array-intersect.php so rather than merge the two arrays do an intersection. Mar 2, 2015 at 15:20
  • I did not read that from your question, maybe you should edit it to clarify. As Woolfie said, you are not looking for merge/union, but for intersection. Mar 2, 2015 at 15:46

easymoden00b,please try this

->addAttributeToFilter('category_id', array('in' => array('finset' => 773,1550)))
  • I ran the resultant toString() on the collection through my mysql application and it appears that your methodology doesn't filter the collection at all. Feb 27, 2015 at 14:58
  • let me check...
    – Amit Bera
    Feb 27, 2015 at 14:59
  • Sorry I used Mage::log((string)$collection->getSelect()); to get the full sql action, but you get it. Feb 27, 2015 at 15:04

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.