We recently dropped a bunch of vendors, and need to get the disabled products out of the categories to make merchandising easier. Also just for sanity so we don't have to keep looking at them in the category product lists.
Here is the script I came up with
$category = Mage::getModel('catalog/category');
$catTree = $category->getTreeModel()->load();
$catIds = $catTree->getCollection()->getAllIds();
$cats = array();
if ($catIds){
foreach ($catIds as $id){
$cat = Mage::getModel('catalog/category');
$cat->load($id);
$collection = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($cat)
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED)) //filter commented, show all products
->load();
foreach ($collection as $prod) {
var_dump("Removing product id " . $prod->getId() . " from category " . $id);
Mage::getSingleton('catalog/category_api')->removeProduct($id,$prod->getId());
}
}
}
Obviously this will take forever, since we have umpteen bazillion categories. I need something that works like this:
Get Category Get all disabled products, and create an array of their IDs. Set the array of products on the category to that array of product ids. Save the category.
Right now i'm digging through
\app\code\core\Mage\Catalog\Model\Category.php
and
\app\code\core\Mage\Catalog\Model\Category\Api.php
to find a function i might be able to use.
Any takers?