4

我正在使用以下脚本来更新我的库存。更新后我想清理缓存并重新索引数据,因为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

4 回答 4

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

愿这对你有帮助!

于 2012-10-03T11:24:38.963 回答
1

也许在您认为合适的时候使用 cron (help: link 1 , link 2 , link 3 ) 运行。/shell/indexer.php

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

至于让 cron 与您的设置一起工作,我建议您找到一些教程,因为它在不同的环境中可能会有所不同。

于 2012-10-03T09:27:59.433 回答
0

你应该尝试reindexEverything()Mage_index_Model_Process

于 2012-10-03T09:23:11.213 回答
0

给出的答案是两个有效的解决方案,这是第 3 个解决方案,在使用数以万计的产品时可能会变得有趣。

如果您有很多产品,重新索引可能会花费一些日志时间,这里有两种方法可以减少它:

  1. 使用magmi进口商。第一个原因:它可以做你的脚本做的事情,你可以用 magmi 替换它。我将它与 magento 1.7.0.2 一起使用。第二个原因:它有一个动态索引器,它会自动更新 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']]));
    
于 2013-05-13T11:09:30.587 回答