[英]Filter product collection on two categories 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
這是因為集合中有一個重復的行。 我正在努力尋找合適的代碼來過濾掉集合中的任何重復行。 解決方案必須是對值進行分組,或使用不同的,但我不知道如何前進。
好的,我已經通過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。
相同的錯誤鏈接建議http://www.magentocommerce.com/boards/m/viewthread/245112/
這在集合中你會有同樣的id
所以我在收集結束時使用下面的代碼
$收藏 - > getSelect() - >不同的(真);
這將使選擇明顯
我收到此錯誤,Magento通過'/ var / reports / xxx'報告的是:
a:5:{i:0;s:71:"Item (Mage_Catalog_Model_Product) with the same id "xxx"
我想出的是用這個id去激活產品並修復它。 然后我刪除了這個產品並重新創建它。 不是一個完美的解決方案,但現在有效。 但我仍然想知道那里的問題是什么? 在我們的例子中,這個錯誤突然來了。 我們最近沒有改變或開發任何可以歸咎於我們的新東西。 有沒有人知道為什么會發生這種情況?
使用多個類別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.