MAGENTO:如何在更新后重新编制数据索引?

时间:2012-10-03 09:01:40

标签: php magento reindex

我使用以下脚本来更新我的广告资源。更新后,我想清理缓存并重新索引数据,因为qty上的更新值未在产品页面中设置。我怎样才能做到这一点?

$mageFilename = '../app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

set_time_limit(0);
ini_set('memory_limit','1024M');

/***************** UTILITY FUNCTIONS ********************/
function _getConnection($type = 'core_read'){
    return Mage::getSingleton('core/resource')->getConnection($type);
}

function _getTableName($tableName){
    return Mage::getSingleton('core/resource')->getTableName($tableName);
}

function _getAttributeId($attribute_code = 'price'){
    $connection = _getConnection('core_read');
    $sql = "SELECT attribute_id
                FROM " . _getTableName('eav_attribute') . "
            WHERE
                entity_type_id = ?
                AND attribute_code = ?";
    $entity_type_id = _getEntityTypeId();
    return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
}

function _getEntityTypeId($entity_type_code = 'catalog_product'){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
    return $connection->fetchOne($sql, array($entity_type_code));
}

function _checkIfSkuExists($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    $count      = $connection->fetchOne($sql, array($sku));
    if($count > 0){
        return true;
    }else{
        return false;
    }
}

function _getIdFromSku($sku){
    $connection = _getConnection('core_read');
    $sql        = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
    return $connection->fetchOne($sql, array($sku));
}

function _updateStocks($data){
    $connection     = _getConnection('core_write');
    $sku            = $data[0];
    $newQty         = $data[1];
    $productId      = _getIdFromSku($sku);
    $attributeId    = _getAttributeId();

    $sql            = "UPDATE " . _getTableName('cataloginventory_stock_item') . " csi,
                       " . _getTableName('cataloginventory_stock_status') . " css
                       SET
                       csi.qty = ?,
                       csi.is_in_stock = ?,
                       css.qty = ?,
                       css.stock_status = ?
                       WHERE
                       csi.product_id = ?
                       AND csi.product_id = css.product_id";
    $isInStock      = $newQty > 0 ? 1 : 0;
    $stockStatus    = $newQty > 0 ? 1 : 0;
    $connection->query($sql, array($newQty, $isInStock, $newQty, $stockStatus, $productId));
}
/***************** UTILITY FUNCTIONS ********************/

$csv                = new Varien_File_Csv();
$data               = $csv->getData('stocks.csv'); //path to csv
array_shift($data);

$message = '';
$count   = 1;
foreach($data as $_data){
    if(_checkIfSkuExists($_data[0])){
        try{
            _updateStocks($_data);
            $message .= $count . '> Success:: Qty (' . $_data[1] . ') of Sku (' . $_data[0] . ') has been updated. <br />';

        }catch(Exception $e){
            $message .=  $count .'> Error:: while Upating  Qty (' . $_data[1] . ') of Sku (' . $_data[0] . ') => '.$e->getMessage().'<br />';
        }
    }else{
        $message .=  $count .'> Error:: Product with Sku (' . $_data[0] . ') does\'t exist.<br />';
    }
    $count++;
}
echo $message;

我正在尝试这个,但更新速度很慢。我在echo $message.

之前执行了这个脚本
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');

4 个答案:

答案 0 :(得分:3)

使用以下代码在根文件夹中创建一个文件,并从http://www.domain.com/indexing.php

等浏览器运行它
<?php

// when you get status "Processing" instead of "Ready" in your index management run    this script and it's working fine
 // change the index order as per your requirement currently it's 7 for Catalog Search Index  you can set as per your requirement

 require_once 'app/Mage.php';
 umask( 0 );
 Mage :: app( "default" );

 $indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
 foreach ($indexingProcesses as $process) {
  $process->reindexEverything();
}

?>

或将此代码放入您的代码中

 $indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
 foreach ($indexingProcesses as $process) {
  $process->reindexEverything();
}

这可能会对你有所帮助!

答案 1 :(得分:1)

也许使用cron(帮助:link 1link 2link 3)来运行您认为合适的/shell/indexer.php

索引确实需要一段时间。您经历的缓慢更新很自然。当您从后端更新索引时会发生同样的事情,毕竟它是相同的过程。

至于让cron使用你的设置,我建议你找一些教程,因为它可以在不同的环境中有所不同。

答案 2 :(得分:0)

您应该从reindexEverything()尝试Mage_index_Model_Process而不是

答案 3 :(得分:0)

给出的答案是两个有效的解决方案,这是第三个在使用一些产品时可能会变得有意义的解决方案。

如果您有许多产品,重新索引可能需要一个记录时间,这有两种方法可以减少它:

  1. 使用magmi导入程序。第一个原因:它可以做你的脚本所做的事情,你可以用magmi替换它。我正在使用它与magento 1.7.0.2。第二个共鸣:它有On the fly indexer自动更新url_rewrites和category_products索引,即使你更新所有产品,它仍然比完全重新索引更快。

  2. Magento可以重新索引单个产品,这比完全重新索引更快,特别是如果您进行增量导入。我为products_price_index执行此操作

    Mage::app('admin')->setUseSessionInUrl(false);
    Mage::getConfig()->init();
    $product_indexer_price = Mage::getResourceSingleton('catalog/product_indexer_price');
    $product_indexer_price->reindexProductIds(array($this->m_products[$res['products_id']]));