문제

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