15

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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
dido
  • 2,330
  • 8
  • 37
  • 54

5 Answers5

48

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();
dmanners
  • 2,062
  • 1
  • 18
  • 24
3

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
  • 2
    This is just explaining what he's already doing. The OP wants to know how to do it programatically. – scrowler Oct 02 '15 at 01:43
1
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

Sackline
  • 31
  • 4
1

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.

0
 <?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

ManiMaran A
  • 309
  • 3
  • 6
  • Welcome to Stack Overflow! Please don't answer just with links and source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Aug 06 '18 at 08:17