我想获得包含 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
这是因为集合中有重复的行。我正在努力寻找正确的代码来过滤掉集合中的任何重复行。解决方案必须是对值进行分组,或者使用不同的值,但我不确定如何进行。
另见 Filter Magento collection but not products, using distinct
最佳答案
好的,感谢 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。
关于php - 过滤两个类别的产品集合 Magento 1.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21120381/