【问题标题】:MAGENTO: Reindexing price programmaticallyMAGENTO:以编程方式重新索引价格
【发布时间】:2013-02-12 08:02:36
【问题描述】:

我以编程方式更新 magento 中的价格。我如何在此更新后重新索引价格。现在我使用了 SSH 命令:

php indexer.php --reindex catalog_product_price

【问题讨论】:

  • 那个 ssh 命令有什么问题?

标签: magento reindex


【解决方案1】:

以下将重新索引每个索引。

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

您还可以使用 Magento 集合模型来加载每个索引,而不是在 for 循环中硬编码 id。

/* @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();
}

但如果你只想重新索引 id 为 2 的价格

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

您也可以按如下方式调用函数 getProcessByCode:

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

【讨论】:

  • 很高兴能为您找到答案。
  • 我不喜欢循环中的“9”。你有替代方案吗?
  • 我已经更新了答案,包括通过集合加载索引进程。
  • 感谢您的精彩回答。也为我工作。
  • @ThangPham 如果您能够通过 ssh 运行 indexer.php 然后使用它,但如果您想在模型或控制器中重新索引,请使用上面的 php。
【解决方案2】:

通过使用以下 SSH 命令,您可以重新索引所有索引。

php shell/indexer.php reindexall

但如果您只想重新索引 catalog_product_price 那么您可以使用以下代码。

php shell/indexer.php --reindex catalog_product_price

【讨论】:

  • 这只是解释他已经在做什么。 OP 想知道如何以编程方式进行。
【解决方案3】:
php -f indexer.php help

您可以将此命令用于所有与通过 SSH 重新索引相关的命令。

php indexer.php -- reindex [process_code]

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

如果你喜欢代码方式,那些是通过 SSH 的,那么你必须通过下面的代码:

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

或者这样做:

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

For More Info

【讨论】:

    【解决方案4】:

    如果您有平面表并想知道为什么(就像我今天所做的那样)无论您重新索引多少次,前端都不会显示以编程方式更新的价格,那么您很可能需要在重新索引价格后重新索引产品平面:

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

    如果你做一个正常的:

    php shell/indexer.php reindexall
    

    注意重新索引的顺序:

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

    产品平面在价格之前被索引,因此价格更新未在平面表中更新(即catalog_product_flat_2)。查看平面表以确保设置了以编程方式更新的价格。

    【讨论】:

      【解决方案5】:
       <?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 => 运行 reindex all

      http://www.webizon.in/apiconnector/index/reindex/indexer_id => 运行单独的重新索引

      (例如:http://www.webizon.in/apiconnector/index/reindex/catalog_product_price)- 运行价格索引

      【讨论】:

      • 欢迎来到 Stack Overflow!请不要只用链接和源代码回答。尝试对您的解决方案如何工作提供一个很好的描述。见:How do I write a good answer?。谢谢
      猜你喜欢
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多