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

发布于 2024-09-12 20:38:00 字数 1311 浏览 3 评论 0原文

我正在尝试寻找分为两类的产品。 我找到了一个获取类别 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 合并?

谢谢

谢谢

I'm trying to find products that are in two categories.
I've found an example to get products that are in category1 OR category2.
http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html
I need products that are in category1 AND category2.

The example in the blog is:

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;

  }
}

When I'm using this filter alone it perform OR query on several categories.
When I combine this filter with prepareProductCollection of Mage_Catalog_Model_Layer
it somehow remove the filter effect.

How can I change the filter to AND and combine it with prepareProductCollection?

Thanks

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

节枝 2024-09-19 20:38:00

此代码将允许您按多个类别进行过滤,但如果您必须执行多个集合加载,则可以避免完全降低性能:

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

This code will allow you to filter by multiple categories but avoid completely killing performance if you had to perform multiple collection loads:

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

The first step is to get a collection of products for one category (in this case, a Featured category). Next step is to get the IDs of the products, notice that this does NOT perform a load (ref Mage_Core_Model_Mysql4_Collection_Abstract::getAllIds())

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

Then get the IDs for a second category:

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

And intersect the arrays to find product IDs that exist in both categories:

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

For this particular use case, we loop until we have sufficient intersecting products, traversing up the category tree until we find a large enough match (but stopping at root category!):

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

Finally, filter our initial collection by the IDs of the intersecting products, and return.

    $aFeaturedItems = $aFeaturedCollection->addIdFilter(array_slice($aMergedProdIds,0,$iNumberFeaturedItems))->getItems();
    return $aFeaturedItems;
瑾兮 2024-09-19 20:38:00

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

有一种可能的解决方案,但需要我发现的覆盖功能 这里

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

I am also working on this to no avail, it was available in magento 1.3 using the attribute filter with finset on the category_ids column, however this was moved into the index table from the entity table and now no longer works.

There is one possible solution, but requries an override function which I found here

But this solution is far from ideal.

2024-09-19 20:38:00

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

I think that it will be enough to call addCategoryFilter() twice on your product collection - once for each category. I have not tested it though, so might be wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文