0

I want to use Dataflow-Advanceed profile to import the csv from frontend. i have googled and find a stand-alone script for that, but problem is that now this script work only for fixed csv file. I want to change the csv file name on run time to import through stand-alone script. Please refer screen-shot link https://www.diigo.com/item/image/5hhy6/7pxw to more clear. Could anyone help me to get achieve this?

Here is sample script:

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

Looking forward for positive response!

Thanks in advance!

1

I got the solution.

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

Hope this helps someone!

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.