过滤Magento 1.7两类产品

时间:2014-01-14 17:48:03

标签: php magento collections magento-1.7

我希望获得A类或B类产品的产品系列。我已经能够成功地使用以下php代码获取这些产品:

$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类,则会显示错误:

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

这是因为集合中有一个重复的行。我正在努力寻找合适的代码来过滤掉集合中的任何重复行。解决方案必须是对值进行分组,或者使用distinct,但我不确定如何进行。

另见Filter Magento collection but not products, using distinct

4 个答案:

答案 0 :(得分:13)

好的,由于https://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。

答案 1 :(得分:2)

相同的错误链接建议http://www.magentocommerce.com/boards/m/viewthread/245112/

在收藏中你会有相同的身份

所以我在收集结束时使用了以下代码

$收藏 - > getSelect() - >不同(真);

这将使选择不同

答案 2 :(得分:1)

我收到了这个错误以及Magento通过&var; / var / reports / xxx'是:

a:5:{i:0;s:71:"Item (Mage_Catalog_Model_Product) with the same id "xxx"

我想到的是使用此ID停用产品并将其修复。然后我删除了这个产品并重新创建它。不是一个完美的解决方案,但现在有效。 但我仍然想知道那里的问题是什么?在我们的例子中,这个错误突然来了。我们最近没有改变或开发任何新的东西,我们可以将其归咎于此。有没有人知道为什么会发生这种情况?

答案 3 :(得分:0)

使用多个类别ID过滤产品系列

$all_categories = array('3','13','113');   
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->joinField('category_id', 'catalog/category_product', 'category_id', 
                    'product_id = entity_id', null, 'left')
                  ->addAttributeToSelect('*')
                  ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                  ->addAttributeToFilter('category_id', array($all_categories));
foreach($productCollection as $product)
{
    echo $product->getId() .$product->getName() . "<br/>";
}
相关问题