在 Magento 中按两个类别过滤产品集合
我正在尝试寻找分为两类的产品。 我找到了一个获取类别 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此代码将允许您按多个类别进行过滤,但如果您必须执行多个集合加载,则可以避免完全降低性能:
第一步是获取一个类别(在本例中为特色类别)的产品集合。下一步是获取产品的 ID,请注意,这不执行加载(参考
Mage_Core_Model_Mysql4_Collection_Abstract::getAllIds()
),然后获取第二个类别的 ID :
并与数组相交以查找两个类别中都存在的产品 ID:
对于此特定用例,我们循环直到有足够的相交产品,向上遍历类别树,直到找到足够大的匹配项(但在根类别停止!) :
最后,通过相交产品的 ID 过滤我们的初始集合,然后返回。
This code will allow you to filter by multiple categories but avoid completely killing performance if you had to perform multiple collection loads:
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()
)Then get the IDs for a second category:
And intersect the arrays to find product IDs that exist in both categories:
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!):
Finally, filter our initial collection by the IDs of the intersecting products, and return.
我也在努力解决这个问题,但没有成功,它在 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.
我认为在您的产品集合上调用 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.