我想使用Dataflow-Advanced配置文件从前端导入csv。我已经google搜索并找到了一个独立的脚本,但问题是现在这个脚本只适用于固定的csv文件。我想在运行时更改csv文件名以通过独立脚本导入。请更清楚地参考屏幕截图链接https://www.diigo.com/item/image/5hhy6/7pxw?size=o。谁能帮助我实现这个目标呢?
以下是示例脚本:
require_once 'app/Mage.php';
umask(0);
ini_set("memory_limit","1024M");
Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_ADMINHTML,
Mage_Core_Model_App_Area::PART_TRANSLATE);
setlocale(LC_ALL, 'en_US');
$localeName = "en_US";
Mage::app()->getLocale()->setDefaultLocale($localeName);
Mage::app()->getLocale()->setLocale($localeName);
Mage::app()->getLocale()->setLocaleCode($localeName);
setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode());
Mage::app()->setCurrentStore(0);
Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_ADMINHTML, Mage_Core_Model_App_Area::PART_TRANSLATE);
Mage::app();
$profileId = 11; //put your profile id here
$logFileName= date("j-n-Y")."-import.log";
$recordCount = 0;
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());
$adapter->setBatchParams($batchModel->getParams());
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 . ' - Error - ' . $ex->getMessage(),null,$logFileName);
}
}
foreach ($profile->getExceptions() as $e) {
Mage::log($e->getMessage(),null,$logFileName);
}
}
}
Mage::log("Import Completed",null,$logFileName);
期待积极的回应!
提前致谢!
答案 0 :(得分:0)
我得到了解决方案。
require_once 'app/Mage.php';
umask(0);
ini_set("memory_limit","1024M");
Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_ADMINHTML,
Mage_Core_Model_App_Area::PART_TRANSLATE);
setlocale(LC_ALL, 'en_US');
$localeName = "en_US";
Mage::app()->getLocale()->setDefaultLocale($localeName);
Mage::app()->getLocale()->setLocale($localeName);
Mage::app()->getLocale()->setLocaleCode($localeName);
setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode());
Mage::app()->setCurrentStore(0);
Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_ADMINHTML, Mage_Core_Model_App_Area::PART_TRANSLATE);
Mage::app();
$profileId = 11; //put your profile id here
$logFileName= date("j-n-Y")."-import.log";
$recordCount = 0;
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');
}
//code to change the csv file name :: START
//echo "<pre>"; print_r($profile->getData('actions_xml')); echo "</pre>";
$str = $profile->getData('actions_xml');
$newCsv = 'import_products_new.csv'; // put your logic to get new file name as per your requirement
$new_action_xml = str_replace('import_products_old.csv', $newCsv, $str); //'import_products_old.csv' using static, as we know filename in already saved profile
$profile->setActionsXml($new_action_xml);
//code to change the csv file name :: END
}
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());
$adapter->setBatchParams($batchModel->getParams());
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 . ' - Error - ' . $ex->getMessage(),null,$logFileName);
}
}
foreach ($profile->getExceptions() as $e) {
Mage::log($e->getMessage(),null,$logFileName);
}
}
}
Mage::log("Import Completed",null,$logFileName);
希望这有助于某人!
谢谢!