0

I’m writing a little script to hopefully pull a collection of products which match certain criteria - so far its working at its most basic using:

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', 1);
$products->addAttributeToSelect('*');

What I am struggling with is creating a filter which will only pull products into the collection if they are not assigned to any categories.

I have been tearing my hair out searching through any docs I can find but cant seem to get my head around it - I can use getAvailableInCategories() after but looping through 25,000 products is time consuming and there must be a better way to do it.

Any help appreciated - im a complete newbie smile

1

If you take a look at monojit's answer here you'll see in order to make the collection item aware of it's category, you'll need to join in the category_product table. So your collection would look like this:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$collection->addAttributeToFilter('category_id', array('null' => true));
$collection->addAttributeToFilter('status', 1);
$collection->addAttributeToSelect('*');
1
  • This worked great on 2 out of 3 test sites, but the third produced a "ERROR: Item (Mage_Catalog_Model_Product) with the same id "
    – Sam
    Apr 17 '14 at 20:39

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.