5

Actually, I want to load the product collection except for some products. I have the product ids that I don't want in the collection. For example, the product ids that I don't want in the product collection is like

$productIds = array(10,20,30); 

How can I do this?

1
  • $productCollection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToFilter('entity_id', array('nin' => $productIds)); Commented Sep 22, 2016 at 13:16

3 Answers 3

5

You can use this code.

$productIds = array(10,20,30); $collection = Mage::getModel('catalog/product')->getCollection() ->addFieldToFilter('entity_id', array('nin' => $productIds)); 
1
  • Thanks, Dhiren . It works fine with [->addFieldToFilter('entity_id', array('nin' => $productIds));] Commented Sep 22, 2016 at 13:37
5

Use addIdFilter() with the second parameter $exclude set to true, to invert the filter:

$productCollection->addIdFilter($productIds, true) 
4

some time not in not work without addFieldToSelect I will always use this

$productIds = array(10,20,30) $collection = Mage::getModel('catalog/product')->getCollection() ->addFieldToSelect('entity_id') ->addFieldToFilter('entity_id', array('nin' => $productIds)); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.