Search code examples
magentocategoriesproductobservers

How to disable products that are not assigned to a category?


Could someone assist with this issue I am having please... I need to disable all products that are not assigned to a category in Magento but the problem is there are some 10,000+ products that I will need to sort through.

I would like to ask what the best approach would be so I could at least begin to solve the problem.

Would it be possible to set all products to disabled if they are not assigned to a category using an Observer? Should I echo the list of unassigned products in a loop then set the status to disabled...

I'm not sure how to go about this one...


Solution

  • Best way to do this is using Magento collections.

    Create a new PHP file, include Mage.php, initialize the application and make your changes.

    It might take a while depending on product count.

    I think you want to do something like that:

    require_once('../app/Mage.php');
    Mage::init();
    
    $product = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
    $store_id = 1;
    
    foreach ($product as $prod)
    {
        if ($prod->getCategoryIds() == NULL)
        {
    
            Mage::getModel('catalog/product_status')
                     ->updateProductStatus($prod->getId(), $store_id, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
    
        }
    }