在 Magento 中按两个类别过滤产品集合

问题描述 投票:0回答:3

我正在尝试查找分为两类的产品。 我找到了一个获取类别 1 或类别 2 中的产品的示例。 http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html 我需要类别 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;

  }
}

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

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

谢谢

谢谢

php magento filter collections
3个回答
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,请注意,这执行加载(参考

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

对于这个特定的用例,我们循环直到有足够的相交产品,向上遍历类别树,直到找到足够大的匹配(但停在根类别!):

    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;

1
投票

我也在努力解决这个问题,但没有成功,它在magento 1.3中可以使用category_ids列上带有finset的属性过滤器,但是它已从实体表移动到索引表中,现在不再起作用。

有一种可能的解决方案,但需要我在here

找到的覆盖功能

但这个解决方案远非理想。


-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.