在magento中通过cron导入产品时出错

时间:2012-07-28 09:50:20

标签: magento import cron

我正在使用此脚本通过magent中的cron导入产品,一切正常,但它不会导入产品数量(qty)以及产品is_in_stock状态,尽管所有这些都在CSV文件中。请帮助我怎样才能成功导入所有产品信息?导入代码如下:

<?php
 require_once 'app/Mage.php';
umask(0);
 ini_set("memory_limit","1024M");
//$_SERVER['SERVER_PORT']='443';
Mage::app();
$profileId = 7; //put your profile id here

$logFileName= "import.log";
$recordCount = 0;
// This won't work if Logging settings is disabled
// To activate it go to System->Configuration->Developer
Mage::log("Import Started",null,$logFileName);
$profile = Mage::getModel('dataflow/profile');

$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);

if ($profileId) {
    $profile->load($profileId);
    if (!$profile->getId()) {
        Mage::getSingleton('adminhtml/session')->addError('The profile you are trying to save no longer exists');
    }
}

Mage::register('current_convert_profile', $profile);

$profile->run();

$batchModel = Mage::getSingleton('dataflow/batch');
if ($batchModel->getId()) {
    if ($batchModel->getAdapter()) {
        $batchId = $batchModel->getId();
        $batchImportModel = $batchModel->getBatchImportModel();
        $importIds = $batchImportModel->getIdCollection();
        $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
        $adapter = Mage::getModel($batchModel->getAdapter());
        foreach ($importIds as $importId) {
            $recordCount++;
            try{
                $batchImportModel->load($importId);
                if (!$batchImportModel->getId()) {
                    $errors[] = Mage::helper('dataflow')->__('Skip undefined row');
                    continue;
                }

                $importData = $batchImportModel->getBatchData();
                try {
                    $adapter->saveRow($importData);
                } catch (Exception $e) {
                    Mage::log($e->getMessage(),null,$logFileName);
                    continue;
                }

                if ($recordCount%20 == 0) {
                    Mage::log($recordCount . ' - Completed!!',null,$logFileName);
                }
            } catch(Exception $ex) {
                Mage::log('Record# ' . $recordCount . ' - SKU = ' . $importData['sku']. ' - Error - ' . $ex->getMessage(),null,$logFileName);
            }
        }
        foreach ($profile->getExceptions() as $e) {
            Mage::log($e->getMessage(),null,$logFileName);
        }

    }
}

Mage::log("Import Completed",null,$logFileName);

// Catalog Rewrites
try {
    Mage :: getSingleton( 'catalog/url' ) -> refreshRewrites();
}
catch ( Exception $e ) {
    Mage::log($e -> getMessage(),null,$logFileName);
}
// LAYERED NAV
    try {
        $flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();
        if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) {
            $kill = Mage::getModel('catalogindex/catalog_index_kill_flag')->loadSelf();
            $kill->setFlagData($flag->getFlagData())->save();
        }

        $flag->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED)->save();
        Mage::getSingleton('catalogindex/indexer')->plainReindex();
        Mage::log('Layered Navigation Indices were refreshed successfully', null, $logFileName);
    }
    catch (Mage_Core_Exception $e) {
        Mage::log($e -> getMessage(). "\n",null,$logFileName);
    }
    catch (Exception $e) {
        Mage::log('Error while refreshed Layered Navigation Indices. Please try again later', null,$logFileName);
    }
 ?>

由于 爬完

2 个答案:

答案 0 :(得分:1)

$product->save()来电之前加上这个。

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

答案 1 :(得分:0)

为什么不直接使用产品型号进行导入?