Magento addCategoryFilter的多个类别
是否有一个变通方法来操作addCategoryFilter方法,以便它可以过滤多个类别?我已经尝试了下面的代码,但没有成功Magento addCategoryFilter的多个类别,magento,Magento,是否有一个变通方法来操作addCategoryFilter方法,以便它可以过滤多个类别?我已经尝试了下面的代码,但没有成功 class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{ public function addCategoriesFilter($categories){
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'))
您可以将下一个代码插入到扩展收集方法中(其中,
$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()
);