Magento:按两个类别筛选的产品集合

Magento:按两个类别筛选的产品集合,magento,filter,categories,Magento,Filter,Categories,我正在编写一个模块,为我加载一个特色产品列表。所有特色产品都位于自己的类别+隐藏类别“特色”。脚本返回一个错误 在category视图(list.phtml)中,我调用gettopproducts.phtml(这很好): 此行:->addAttributeToFilter('category_id',array('finset'=>'87')应添加第二个类别过滤器(“特色”类别的ID)。但是当我使用这个时,我会得到一个错误。删除此行时:->addAttributeToFilter('catego

我正在编写一个模块,为我加载一个特色产品列表。所有特色产品都位于自己的类别+隐藏类别“特色”。脚本返回一个错误

在category视图(list.phtml)中,我调用
gettopproducts.phtml
(这很好):

此行:
->addAttributeToFilter('category_id',array('finset'=>'87')应添加第二个类别过滤器(“特色”类别的ID)。但是当我使用这个时,我会得到一个错误。删除此行时:
->addAttributeToFilter('category_id',array('finset'=>'87')它工作得很好


我正在使用Magento 1.7.2,我找到了原因。对于Magento 1.4+,类别ID不是报告/产品集合的成员

这是获得它的方法: 替换
->addAttributeToFilter('category_id',array('finset'=>'87')
与:

代码如下所示:

$_productCollection = Mage::getResourceModel('reports/product_collection');
$_productCollection->addAttributeToSelect('*');
$_productCollection->addCategoryFilter($currentCategory);
$_productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$_productCollection->addAttributeToFilter('category_id', array('in' => array('finset' => '87')));        
$_productCollection->load();
return $_productCollection;

对于magento 1.7,这是我的工作原理:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('id')        
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('home_slider', array('neq' => ''))
    ->addAttributeToFilter('home_slider_value', $slide_num)
    ->addStoreFilter();
    //here pass an array() of CATEGORY IDs
    $catids = array('id_1,id_2, etc..'); 

    $statements = array();
    foreach ($catids as $categoryId){
        if (is_numeric($categoryId)){
         $statements[] = "{{table}}.category_id = $categoryId";
        }
    }

    $collection->distinct(true)
    ->joinField('category_id','catalog/category_product', null,'product_id = entity_id', implode(" OR ",$statements),'inner');
$_productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$_productCollection->addAttributeToFilter('category_id', array('in' => array('finset' => '87'))); 
$_productCollection = Mage::getResourceModel('reports/product_collection');
$_productCollection->addAttributeToSelect('*');
$_productCollection->addCategoryFilter($currentCategory);
$_productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$_productCollection->addAttributeToFilter('category_id', array('in' => array('finset' => '87')));        
$_productCollection->load();
return $_productCollection;
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('id')        
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('home_slider', array('neq' => ''))
    ->addAttributeToFilter('home_slider_value', $slide_num)
    ->addStoreFilter();
    //here pass an array() of CATEGORY IDs
    $catids = array('id_1,id_2, etc..'); 

    $statements = array();
    foreach ($catids as $categoryId){
        if (is_numeric($categoryId)){
         $statements[] = "{{table}}.category_id = $categoryId";
        }
    }

    $collection->distinct(true)
    ->joinField('category_id','catalog/category_product', null,'product_id = entity_id', implode(" OR ",$statements),'inner');