过滤产品集合两个类别Magento 1.7 [英] Filter product collection on two categories Magento 1.7

查看:132
本文介绍了过滤产品集合两个类别Magento 1.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得产品集合中的产品类别A或类别B.我已经能够成功地获得这些产品与以下php代码:

I want to get a product collection with products in Category A or Category B. I have been able to succesfully get these products with the following php-code:

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('category_id', array('in' => array('finset' => 4,19)))
    ->addAttributeToSelect('*');

但是,如果产品同时在类别4和19中,则会显示错误: p>

However, if the product is in both category 4 AND 19, then an error is displayed:

Item (Mage_Catalog_Model_Product) with the same id "173" already exist

这是因为集合中有重复的行。我很难找到正确的代码,以过滤掉集合中的任何重复行。解决方案必须是对值进行分组,或使用distinct,但不知道如何去。

This is because the collection has a duplicate row in it. I'm struggling to find the right code to filter out any duplicate rows in the collection. The solution must be to group the values, or use distinct, but I'm not sure how to go forth.

另请参见过滤器Magento收集,但不是产品,使用不同

推荐答案

好,我已经解决了这个问题,感谢 http://stackoverflow.com / a / 13291759/991491

Ok, I've got this solved thanks to http://stackoverflow.com/a/13291759/991491

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
    ->addAttributeToFilter('category_id', array('in' => array('finset' => 3,4)))
    ->addAttributeToSelect('*');
$collection->getSelect()->group('e.entity_id');

group子句在克服重复的产品ID时会遇到问题。

The group clause does the trick in overcoming duplicate product id's that get returned.

这篇关于过滤产品集合两个类别Magento 1.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆