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?

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

You can use this code.

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

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

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

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));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.