Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MAGENTO:更新后如何重新索引数据?_Php_Magento_Reindex - Fatal编程技术网

Php MAGENTO:更新后如何重新索引数据?

Php MAGENTO:更新后如何重新索引数据?,php,magento,reindex,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); M

我正在使用以下脚本更新我的库存。更新后,我想清理缓存并重新索引数据,因为产品页面中未设置
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;

您应该尝试从
Mage\u index\u Model\u进程
重新索引一切()

或者在您认为合适的时候使用cron(help:,)运行
/shell/indexer.php

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


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

使用以下代码在根文件夹中创建一个文件,并从类似浏览器的位置运行它


愿这对你有帮助

给出的答案是两个有效的解决方案,下面是第三个解决方案,在使用一些十分之一的产品时可能会变得有趣

如果您有许多产品,重新编制索引可能需要一段日志时间,以下是两种减少此时间的方法:

  • 使用进口商。第一个原因:它可以做你的脚本所做的,你可以用magmi代替它。我将它与magento 1.7.0.2一起使用。第二个原因:它有一个自动更新url和category产品索引的功能,即使你更新了所有产品,它仍然比完全重新索引快

  • Magento可以对单个产品重新编制索引,这比完全重新编制索引要快,尤其是在增量导入的情况下。我这样做是为了产品价格指数

    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']]));
    
  • <?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();
    }
    
    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']]));