2012-10-04 21 views
0

是否有一種解決方法來操作addCategoryFilter方法,以便它可以過濾多個類別?我已經嘗試了下面的這段代碼,但它沒有奏效。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')) 

回答

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() 
    );