Skip to content

Magento : Filter product collection by multiple categories

this is an interesting post i found on StackExchange, how to filter product collection by multiple categories, here

the code

 

$productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField(
        'category_id', 'catalog/category_product', 'category_id', 
        'product_id = entity_id', null, 'left'
    )
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_id', array(
            array('finset' => array('7', '8', )),
    ))
    ->addAttributeToSort('created_at', 'desc');

foreach($productCollection as $product){
    echo $product->getId() . "<br/>\n";
};

source : http://magento.stackexchange.com/questions/7094/filter-product-collection-by-multiple-categories

Share

Comments are closed.