Collections 在Magento中按两个类别筛选产品集合

Collections 在Magento中按两个类别筛选产品集合,collections,magento,filter,Collections,Magento,Filter,我试图找到两类产品。 我发现了一个示例,可以获得类别1或类别2中的产品。 我需要1类和2类的产品 博客中的例子是: class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{ public function addCategoriesFilter($categories){

我试图找到两类产品。 我发现了一个示例,可以获得类别1或类别2中的产品。 我需要1类和2类的产品

博客中的例子是:

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;

  }
}
当我单独使用这个过滤器时,它会对几个类别执行或查询。 当我将此过滤器与Mage_Catalog_Model_层的prepareProductCollection结合使用时 它以某种方式消除了过滤效果

如何将筛选器更改为和,并将其与prepareProductCollection组合

谢谢


谢谢

我认为在您的产品集合中调用addCategoryFilter()两次就足够了,每个类别一次。我还没有测试过它,所以可能是错误的。

我也在做这件事,但没有用,它在magento 1.3中使用了category_id列上带有finset的属性过滤器,但是它被从实体表移到了索引表中,现在不再工作了

有一种可能的解决方案,但是需要一个覆盖函数,我发现了这个函数


但此解决方案远非理想。

此代码允许您按多个类别进行筛选,但如果您必须执行多个集合加载,则可避免完全破坏性能:

$iNumberFeaturedItems = 4;
$oCurrentCategory = Mage::registry('current_category');
$oFeaturedCategory = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToFilter('name','Featured')
        ->getFirstItem();
$aFeaturedCollection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect(array('name', 'price', 'small_image', 'url_key'), 'inner')
        ->addStoreFilter()
        ->addCategoryFilter($oFeaturedCategory)
        ->addCategoryIds();
第一步是获取一个类别(在本例中为特色类别)的产品集合。下一步是获取产品的ID,注意这不会执行加载(ref
Mage\u Core\u Model\u Mysql4\u Collection\u Abstract::getAllIds()

然后获取第二个类别的ID:

    $aCurrentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds();
并将数组相交以查找两个类别中存在的产品ID:

    $aMergedProdIds = array_intersect($aFeaturedProdIds,$aCurrentCatProdIds);
对于这个特定的用例,我们循环直到有足够多的相交产品,遍历类别树直到找到足够大的匹配项(但在根类别处停止!):


不幸的是,对addCategoryFilter()的第二次调用覆盖了第一次调用。我认为您的链接使用了旧模式,其中所有类别都存储在csv字段中,就像现在的multi-select属性一样。在django中,这是一行代码,但在magento中,一切都很困难。解决方案是在我之前应该注意的注释中,很难开始工作,而且不能立即工作。
    $aMergedProdIds = array_intersect($aFeaturedProdIds,$aCurrentCatProdIds);
    while(count($aMergedProdIds) < $iNumberFeaturedItems && $oCurrentCategory->getId() != Mage::app()->getStore()->getRootCategoryId()): 
        $oCurrentCategory = $oCurrentCategory->getParentCategory();
        $aParentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds();
        $aMergedProdIds = array_intersect($aFeaturedProdIds,$aParentCatProdIds);
    endwhile;
    $aFeaturedItems = $aFeaturedCollection->addIdFilter(array_slice($aMergedProdIds,0,$iNumberFeaturedItems))->getItems();
    return $aFeaturedItems;