1

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?

1 Answer 1

1

This works fine. Seems like this is about as good as it gets.

<?php
// Instantiate magento 
umask(0);
require_once '../app/Mage.php';
Mage::app('admin');

// Disable the indexers
// It will try to reindex every time a category is changed. Halts the process.
$pCollection = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
foreach ($pCollection as $process) {
    $process->setMode(Mage_Index_Model_Process::MODE_MANUAL)->save();
    //$process->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();
}

$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()
            ->addAttributeToSelect('position')
            ->addCategoryFilter($cat)
            ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)) 
            ->load();

        $prodPositions = array();
        $existingPositions = $cat->getProductsPosition();
        foreach($collection as $product) {
            // Preserve existing position, or assign 0;                
            $prodPositions[$product->getId()] = (int)$existingPositions[$product->getId()];
        }

        $cat->setPostedProducts($prodPositions);
        $cat->save();
        var_dump("Updated Category ID " . $id);
    }
}

// Re-enable indexers
foreach ($pCollection as $process) {
    $process->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();
    $process->reindexAll();
}

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.