2016-04-06 4 views
0

フロントエンドからcsvをインポートするためにDataflow-Advancedプロファイルを使用したいと思います。私はグーグルして、それのためのスタンドアロンのスクリプトを見つけるが、問題は今このスクリプトは固定csvファイルのためだけに働くことです。スタンドアロンスクリプトを使用してインポートするために、実行時にCSVファイル名を変更したい。スクリーンショットのリンクhttps://www.diigo.com/item/image/5hhy6/7pxw?size=oを参照してください。誰も私がこれを達成するのを助けることができますか?ここでプロファイルアクションでcsvファイル名変数を変更する方法実行時にDataflow-Advancedプロファイルのxml?

は、サンプルスクリプトです:

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

私は解決策を得ました。

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

希望すると、誰かに役立ちます。

ありがとうございます!

関連する問題