Get products which exists in two categories

106 views Asked by At

I need to find the products that exists in category 93 AND 97. So far i am able to get products that exists in category 93 OR 97, but no luck with AND instead.

Im really clueless right now, and have done a lot of google search now.

Any help is appreciated!

Code so far:

$collection = Mage::getModel('catalog/product')->getCollection(); 
        $collection->distinct(true); 
        $collection->joinField('category_id', 
        'catalog/category_product', 
        'category_id', 
        'product_id=entity_id', 
        null, 
        'left'); 
        $collection->addAttributeToFilter('category_id', array( 'in' =>
        array('finset'=> '93'),
        array('finset'=> '97')));

Magento Enterprise version 1.13.0.0

2

There are 2 answers

1
MagePal Extensions On BEST ANSWER

Take a look @ How to get products that are found in 2 or more categories

$collection = Mage::getModel('catalog/product')->getCollection(); 
....
$cat_ids = array(93,97);  
$collection->addAttributeToFilter('category_id', array('in' => array('finset' => $cat_ids)))
0
jmm On

Think the syntax maybe the problem. Try

$collection->addAttributeToFilter('category_id', array( 'in' => array(93,97))),