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));
    – Vigna S
    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));] 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));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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