Question

I update prices in magento programmatically. How can I reindexing prices after this update. Now I used SSH command:

php indexer.php --reindex catalog_product_price
Was it helpful?

Solution

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();

OTHER TIPS

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
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.

 <?php
   namespace Webizon\ApiConnector\Controller\Index;
   class Reindex extends \Magento\Framework\App\Action\Action {     
/**
 * @var \Magento\Indexer\Model\IndexerFactory
 */
protected $indexerFactory;
/**
 * @var \Magento\Framework\Indexer\ConfigInterface
 */
protected $config;
/**
 * @param Context $context
 * @param \Magento\Indexer\Model\IndexerFactory $resultLayoutFactory    
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
 */
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Indexer\Model\IndexerFactory $indexerFactory,
    \Magento\Framework\Indexer\ConfigInterface $config
) {     
    $this->indexerFactory = $indexerFactory;
    $this->config = $config;
    parent::__construct($context);
}
/**
 * Regenerate full index
 *
 * @return void
 * @throws \Exception
 */
public function execute()
{           
    $params = $this->getRequest()->getParams();
    if(isset($params['run'])){
        if($params['run'] == 'all'){
            $this->reindexAll(); 
        }else{
            $this->reindexOne($params['run']);
        }   
    }           
}
/**
 * Regenerate single index
 *
 * @return void
 * @throws \Exception
 */
private function reindexOne($indexId){
    $indexer = $this->indexerFactory->create()->load($indexId);
    $indexer->reindexAll();
}
/**
 * Regenerate all index
 *
 * @return void
 * @throws \Exception
 */
private function reindexAll(){
    foreach (array_keys($this->config->getIndexers()) as $indexerId) {          
        $indexer = $this->indexerFactory->create()->load($indexerId);
        $indexer->reindexAll();            
    }
}
  }

http://www.webizon.in/apiconnector/index/reindex/all => to run reindex all

http://www.webizon.in/apiconnector/index/reindex/indexer_id => to run separate reindexing

(Ex: http://www.webizon.in/apiconnector/index/reindex/catalog_product_price) - To run price indexing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top