Magento filter product collection by multiple categories

前端 未结 6 1711
故里飘歌
故里飘歌 2020-12-09 13:56

Is there an easy way to filter a product collection by multiple categories? To get all items in any of the listed categories? addCategoryFilter doesn\'t seem to

6条回答
  •  时光取名叫无心
    2020-12-09 14:05

    Filter Product Collection using multiple category ids

    $all_categories = array('3','13','113');   
    $productCollection = Mage::getModel('catalog/product')->getCollection();
    $productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 
                        'product_id = entity_id', null, 'left')
                      ->addAttributeToSelect('*')
                      ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                      ->addAttributeToFilter('category_id', array($all_categories));
    foreach($productCollection as $product)
    {
        echo $product->getId() .$product->getName() . "
    "; }

    You can remove the condition for product type i.e type_id or modify it as per requirement.

提交回复
热议问题