addCategoryFilter的多个类别

时间:2012-10-04 16:02:40

标签: magento

是否有解决方法来操作addCategoryFilter方法,以便它可以过滤多个类别?我已尝试使用下面的代码,但它无效。

class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{

public function addCategoriesFilter($categories){

$alias = 'cat_index';
$categoryCondition = $this->getConnection()->quoteInto(
$alias.'.product_id=e.entity_id AND '.$alias.'.store_id=? AND ',
$this->getStoreId()
);

$categoryCondition.= $alias.'.category_id IN ('.$categories.')';

$this->getSelect()->joinInner(
array($alias => $this->getTable('catalog/category_product_index')),
$categoryCondition,
array('position'=>'position')
);

$this->_categoryIndexJoined = true;
$this->_joinFields['position'] = array('table'=>$alias, 'field'=>'position' );

return $this;

}
}

此外,我在下面尝试过此代码,但它也无效。

->addAttributeToFilter('category_ids',array('finset'=>'3','finset'=>'4'))

1 个答案:

答案 0 :(得分:0)

您可以在扩展收集方法中插入下一个代码(其中$collection将为$this) - 或者在之前定义为产品集合的$collection处使用它:

$catIds = array('3', '4');
$tableAlias = 'cat_prod_idx';
$connection = $collection->getResource()->getReadConnection();
$conditions = array(
    "{$tableAlias}.product_id = e.entity_id",
    $connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()),
    $connection->quoteInto("{$tableAlias}.category_id in (?)", $filters)
);
$collection->getSelect()
    ->group('e.entity_id')
    ->join(
        array($tableAlias => $collection->getTable('catalog/category_product_index')),
        join(' AND ', $conditions),
        array()
    );