简体   繁体   English

过滤Magento 1.7两类产品

[英]Filter product collection on two categories Magento 1.7

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: 我希望获得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('*');

However, if the product is in both category 4 AND 19, then an error is displayed: 但是,如果产品同时属于类别4和19,则会显示错误:

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

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. 解决方案必须是对值进行分组,或使用不同的,但我不知道如何前进。

See also Filter Magento collection but not products, using distinct 另请参阅过滤Magento集合,但不使用distinct

Ok, I've got this solved thanks to https://stackoverflow.com/a/13291759/991491 好的,我已经通过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');

The group clause does the trick in overcoming duplicate product id's that get returned. group子句可以克服返回的重复产品ID。

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

this will happen when in collection you have same id 这在集合中你会有同样的id

so i used below code at the end of collection 所以我在收集结束时使用下面的代码

$collection->getSelect()->distinct(true); $收藏 - > getSelect() - >不同的(真);

this will make select distinct 这将使选择明显

I got this error and what Magento reported via '/var/reports/xxx' was: 我收到此错误,Magento通过'/ var / reports / xxx'报告的是:

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

What I come up with was deactivating product with this id and it was fixed. 我想出的是用这个id去激活产品并修复它。 Then I deleted this product and re-create it. 然后我删除了这个产品并重新创建它。 Not a perfect solution, but works for now. 不是一个完美的解决方案,但现在有效。 But I still wonder what was the problem there? 但我仍然想知道那里的问题是什么? In our case, this error came suddenly. 在我们的例子中,这个错误突然来了。 We haven't changed or developed anything new recently on which we could put the blame. 我们最近没有改变或开发任何可以归咎于我们的新东西。 Does anybody have any idea why this happened out of the blue? 有没有人知道为什么会发生这种情况?

Filter Product Collection using multiple category ids 使用多个类别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/>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM