質問

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

役に立ちましたか?

解決

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));
ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top