Magento – Reindex from php code doesn’t work properly

magento-1.9reindex

I've a PHP script that import products from an external XML File using AWS Fast Simple Import, a magento extension.

Every X minutes it call the XML and check if the product is modified or is new, then it updates or create a new product.

The problem is when a product become unavailable. I've already set up the configuration of magento to NOT SHOW the unavailable products but every time the script goes the unavailable products appear on the catalog page.

So I tried to reindex manually the catalog_product_price and if I refresh the catalog page it works correctly.

But when I do it from PHP using this code:

$process = Mage::getModel('index/indexer')->getProcessByCode('catalog_product_price');
$process->reindexAll();

It doesn't work. I mean if I look at the latest index time it works but in the catalog view I still see the unavailable products.

Best Answer

I'd the same problem and I change

$process->reindexAll();

for

$process->reindexEverything();

Here is my code

public function reindex($string = 'all')
{
    /** @var $indexer Mage_Index_Model_Indexer */
    $indexer = Mage::getModel('index/indexer');

    $processes = array();

    if ($string == 'all') {
        $processes = $indexer->getProcessesCollection();
    } else {
        $codes = explode(',', $string);
        foreach ($codes as $code) {
            $process = $indexer->getProcessByCode(trim($code));
            if ($process) {
                $processes[] = $process;
            }
        }
    }

    /** @var $process Mage_Index_Model_Process */
    foreach ($processes as $process) {

        $status = $process->getStatus();

        if (($status!=Mage_Index_Model_Process::STATUS_RUNNING) AND !($process->isLocked())){

            //$process->reindexAll();
            $process->reindexEverything();

        }

    }

}