0

是否有解决方法来操作 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'))
4

1 回答 1

0

您可以将下一个代码插入到您的扩展集合方法中(where $collectionwill be ) - 或者在之前定义为产品集合的$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()
    );
于 2012-10-04T16:35:10.117 回答