2010-08-03 75 views
3

我试图找到两种类别的产品。 我发现了一个例子,以获得类别1或类别2中的产品。 http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html 我需要属于category1和category2的产品。在Magento中过滤两个类别的产品集合

在博客的例子是:

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_Layer 的prepareProductCollection组合时,它以某种方式删除过滤器效果。

如何将过滤器更改为AND并将其与prepareProductCollection组合使用?

感谢

感谢

回答

3

此代码将允许您通过多个类别进行过滤,但要避免彻底查杀性能,如果你要执行多个收集负载:

$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_Core_Model_Mysql4_Collection_Abstract::getAllIds()

$aFeaturedProdIds = $aFeaturedCollection->getAllIds(); 
    shuffle($aFeaturedProdIds); //randomize the order of the featured products 

然后得到的ID为第二类:

$aCurrentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds(); 

与相交阵列发现存在于两类产品ID:

$aMergedProdIds = array_intersect($aFeaturedProdIds,$aCurrentCatProdIds); 

对于这种特殊的使用情况下,我们循环,直到我们有足够的半成品ecting产品,向上遍历目录树,直到我们找到一个足够大的比赛(但根类别停止!):

while(count($aMergedProdIds) < $iNumberFeaturedItems && $oCurrentCategory->getId() != Mage::app()->getStore()->getRootCategoryId()): 
     $oCurrentCategory = $oCurrentCategory->getParentCategory(); 
     $aParentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds(); 
     $aMergedProdIds = array_intersect($aFeaturedProdIds,$aParentCatProdIds); 
    endwhile; 

最后,通过交叉产品的ID过滤我们最初的收集,并返回。

$aFeaturedItems = $aFeaturedCollection->addIdFilter(array_slice($aMergedProdIds,0,$iNumberFeaturedItems))->getItems(); 
    return $aFeaturedItems; 
+0

伟大的解决方案和解释,谢谢。 – gregdev 2013-05-25 22:47:30

-1

我认为,这将是足以称之为addCategoryFilter()在您的产品收集两次 - 一次为每个类别。我还没有测试过,所以可能是错的。

+2

不幸的是,第二次调用addCategoryFilter()会覆盖第一个。 – pablo 2010-08-03 22:05:33

1

我也在使用这个功能,它在magento 1.3中使用category_ids列上的finset属性过滤器,但是它被从实体表中移入索引表中,现在不再有效。

有一个可能的解决方案,但requries我发现here

的重写功能,但这个解决方案很不理想。

+0

我认为你的链接使用旧的模式,其中所有的类别都存储在csv字段中,如现在的多选属性。在Django中它是一行代码,但在magento中一切都很难。 – pablo 2010-10-15 17:50:25

+0

解决方案在我应该注意到的这个评论之前,很难得到工作,并且不能马上工作。 – 2010-10-22 12:56:25