Magento filter product collection by multiple categories

前端 未结 6 1713
故里飘歌
故里飘歌 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:07

    Here's a method that doesn't require modifications to core. It's taken from this post with the addition of the 'group' clause to handle duplicate product records.

    $categories = array(7,45,233);
    
            $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('*')
                ->joinField('category_id',
                    'catalog/category_product',
                    'category_id',
                    'product_id=entity_id',
                    null,
                    'left')
                ->addAttributeToFilter('category_id', array('in' => $categories));
            $collection->getSelect()->group('e.entity_id');
    

提交回复
热议问题