Question

I need to merge two or more categories products and filter the common products from the collection.

Was it helpful?

Solution

Here's one way of doing it.

First, get the products ids from one category:

$productCollection1 = Mage::getResourceModel('catalog/product_collection')
                       ->addCategoryFilter($categoryId1)
                       ->getAllIds();

Then get the products ids from the second category:

$productCollection2 = Mage::getResourceModel('catalog/product_collection')
                       ->addCategoryFilter($categoryId2)
                       ->getAllIds();

Then you can intersect the two arrays of product ids to be sure you get the product ids that are available in both categories:

$finalProductIds = array_intersect($productCollection1, $productCollection2); 

Finally, if you need to get every detail about those products you can create a new collection based on the final array:

$finalProductCollection = Mage::getResourceModel('catalog/product_collection')
                          ->addAttributeToFilter('entity_id', array('in' => $finalProductIds));
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top