我正在寻找一种方法来过滤当前类别和可选子类别在类别页面上返回的产品。到目前为止,我看到的每个解决方案都是“展示类别中的产品 - 一个OR类别-b”。
我需要编辑哪个文件来根据作为查询参数传递的其他可选类别ID过滤产品集合(例如?catfilter=32
)?
答案 0 :(得分:6)
看这里:http://vibrantdrive.com/how-to-filter-magento-products-using-2-or-more-category-filters/
获得第4类和第5类的产品
$_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' => '4'),
array('finset' => '5'))
)
->addAttributeToSort('created_at', 'desc');
获得第4类或第5类产品
$_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('4', '5')),
)
->addAttributeToSort('created_at', 'desc');
答案 1 :(得分:1)
关于错误项目(Mage_Catalog_Model_Product)在/magento/lib/Varien/Data/Collection.php:373中已经存在相同的ID“30674”,我找到了解决方案:
$conditions = array();
foreach ($categoryIds as $categoryId) {
if (is_numeric($categoryId)) {
$conditions[] = "{{table}}.category_id = $categoryId";
}
}
$collection->distinct(true)
->joinField('category_id', 'catalog/category_product', /* 'category_id' */null,
'product_id = entity_id', implode(" OR ", $conditions), 'inner');