Magento:产品系列按两类过滤

时间:2013-02-10 17:09:40

标签: magento filter categories

我正在编写一个模块,为我加载特色产品列表。所有特色产品都坐落在他们自己的类别+隐藏类别“特色”。该脚本给我一个错误。

在类别视图(list.phtml)上,我呼吁gettopproducts.phtml(工作正常):

<?php $currentCategory = Mage::registry('current_category'); ?>
<?php $_products = $this->getTopProducts($currentCategory); ?>
<?php echo $this->__('Available products: ').$_products->count(); ?>

gettopproducts.phtml我调用getTopProducts()的函数Gettopproducts.php传递当前类别。在Gettopproducts.php我有这个:

public function getTopProducts($currentCategory)
{
    $_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addCategoryFilter($currentCategory)
    ->addAttributeToFilter('category_ids',array('finset'=>'87'));
    $_productCollection->load();
    return $_productCollection;
}

此行:->addAttributeToFilter('category_ids',array('finset'=>'87'));应添加第二个类别过滤器(“特色”类别的ID)。但是当我使用它时,我收到一个错误。当我删除此行时:->addAttributeToFilter('category_ids',array('finset'=>'87'));它完美无缺。

我正在使用Magento 1.7.2

2 个答案:

答案 0 :(得分:1)

我找到了原因。对于Magento 1.4+,类别ID不是reports / product_collection的成员。

这是如何获得它的方式: 替换->addAttributeToFilter('category_ids',array('finset'=>'87')); 用:

$_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;

答案 1 :(得分:0)

对于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');