MAGENTO: Reindexing price programmatically

By using following SSH command you can reindex all the indexes.

php shell/indexer.php reindexall

But if you want to reindex only catalog_product_price then you can use below code.

php shell/indexer.php --reindex catalog_product_price

The following will reindex each index.

for ($i = 1; $i <= 9; $i++) {
    $process = Mage::getModel('index/process')->load($i);
    $process->reindexAll();
}

You can also use the Magento collection model to load each index rather than hard coding the id in the for loop.

/* @var $indexCollection Mage_Index_Model_Resource_Process_Collection */
$indexCollection = Mage::getModel('index/process')->getCollection();
foreach ($indexCollection as $index) {
    /* @var $index Mage_Index_Model_Process */
    $index->reindexAll();
}

But if you want to reindex just the price the id is 2

$process = Mage::getModel('index/process')->load(2);
$process->reindexAll();

You could also call the function getProcessByCode as follows:

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

php -f indexer.php help

You can use this command for all command related to reindex through SSH.

php indexer.php -- reindex [process_code]

  e.g: php indexer.php --reindex catalog_product_price

Those are via SSH if you like code ways then you have to go through below code:

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

or do this :

  for ($i = 0; $i <= 8; $i++) {  
       $process = Mage::getModel('index/process')->load($i);  
      $process->reindexAll();  
  } 

For More Info


If you have flat tables on and wonder why (as I did today) programmatically updated prices are not displaying on the front end no matter how many times you reindex, most probably you need to reindex the product flat AFTER you reindex the prices:

php shell/indexer.php -reindex catalog_product_price
php shell/indexer.php -reindex catalog_product_flat

If you do a normal:

php shell/indexer.php reindexall

Note the order of the reindexing:

Category Flat Data index was rebuilt successfully in 00:00:00
Product Flat Data index was rebuilt successfully in 00:00:00
Stock Status index was rebuilt successfully in 00:00:00
Catalog product price index was rebuilt successfully in 00:00:00
...

The product flat is indexed BEFORE the prices, there fore the price updates were not updated in the flat tables (i.e. catalog_product_flat_2). Look in the flat tables to make sure your programmatically updated prices are set.