php - Run Magento 1.9.1.0 Dataflow Import Profile Programmatically -


i've tried working, can't seem find solution. i'm looking run existing dataflow profile has id = 3, , has import file name configured.

magento dataflow profiles

dataflow profile settings

all research i've done, leads variation of following code:

public function importproducts($profile_id = 3) {      require_once('../app/mage.php');      mage::app()->setcurrentstore(mage_core_model_app::admin_store_id);      // instantiate session "root" user.     $usermodel = mage::getmodel('admin/user');     $usermodel->setuserid(0);     mage::getsingleton('admin/session')->setuser($usermodel);      // load dataflow profile.     $profile = mage::getmodel('dataflow/profile');     $profile->load($profile_id);     if (!$profile->getid()) {         exit("profile id #{$profile_id} not exist.");     }      $profile->run();      $batchmodel = mage::getsingleton('dataflow/batch');      // reporting.     $direction = ucwords($profile->getdirection());     $success = "{$direction} id #{$batchmodel->getid()} completed succesfully.\n";      echo $success;      return true; } 

running profile in question (id = 3) magento backend works perfectly, can't seem able trigger php function above.

i'm looking way trigger "import products" dataflow profile programmatically.

other posts, questions, , sites i've come across have had no success them:

any , appreciated!

thank you!

after frustration, here's answer works:

note in case i've configured default magento dataflow profile import products (id: 3) read in xml import format, predefined file. file create needed prior running import operations. (refer screenshot in question above)

once have profile created you'll need 2 files:

  • importer.php
  • batch_importer_processor.php

you can place files in /magento/root/shell/ directory, or adjust paths needed if you're including in separate location. once in directory, can call trigger operation via cron using:

    php -f /path/to/magento/root/directory/shell/importer.php 

the -f parameter above "parse , execute" file being called.

source each file is:

importer.php

<?php               require_once '../app/mage.php';      set_time_limit(0);     ini_set('memory_limit', '128m');      $root       = "/path/to/your/magento/root/directory/";     $logfile    = 'magento_import.log';      umask(0);     $app = mage::app('default');      mage::log("========================== begin import ==========================", null, $logfile);     //echo "========================== begin import ==========================\n";      // login admin user      mage::getsingleton('core/session', array('name' => 'adminhtml'));      $user = mage::getmodel('admin/user')->loadbyusername($username);      if (mage::getsingleton('adminhtml/url')->usesecretkey()) {       mage::getsingleton('adminhtml/url')->renewsecreturls();     }      $session = mage::getsingleton('admin/session');     $session->setisfirstvisit(true);     $session->setuser($user);     $session->setacl(mage::getresourcemodel('admin/acl')->loadacl());     mage::dispatchevent('admin_session_user_login_success',array('user'=>$user));      if ($session->isloggedin()) {          mage::log("user '" . $username . "' logged in.", null, $logfile);         //echo "user '" . $username . "' logged in.\n";      } else {          mage::log("error: not login user '" . $username . "'.", null, $logfile);         //echo "error: not login user '" . $username . "'.\n";      }      // load dataflow profile      $profile_id = 3;      $profile = mage::getmodel('dataflow/profile');      $profile->load($profile_id);      if (!$profile->getid()) {          mage::log("error: profile id #{$profile_id} not exist.", null, $logfile);         //echo "error: profile id #{$profile_id} not exist.\n";         exit;      }      mage::register('current_convert_profile', $profile);      $profile->run();      // begin bactch processing      // limit of products per batch (max: 50)     $batchlimit = 50;      function convert($size)     {          $unit=array('b','kb','mb','gb','tb','pb');          return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];      }      $batchmodel = mage::getsingleton('dataflow/batch');      if (!$batchmodel->getid()) {          mage::log(convert(memory_get_usage()) . " - error: can't batchmodel", null, $logfile);         //echo convert(memory_get_usage()) . " - error: can't batchmodel\n";         exit;      }      if (!$batchmodel->getadapter()) {          mage::log(convert(memory_get_usage()) . " - error: can't getadapter", null, $logfile);         //echo convert(memory_get_usage()) . " - error: can't getadapter\n";         exit;      }      $batchid            = $batchmodel->getid();     $batchimportmodel   = $batchmodel->getbatchimportmodel();     $importids          = $batchimportmodel->getidcollection();      $recordcount        = null;     $totalproducts      = count($importids);      $saved              = 0;     $batcharrayids      = array();      foreach ($importids $importid) {          $recordcount++;          $batcharrayids[] = $importid;          if ($recordcount%$batchlimit == 0 || $recordcount == $totalproducts) {              $paramsarr  = array('batchid' => $batchid, 'ids' => $batcharrayids);             $params     = json_encode($paramsarr);             $result     = array();              exec("php -f {$root}shell/batch_import_processor.php '{$params}'", $result);              $saved += $result[0];              mage::log(convert(memory_get_usage()) . " - processed {$recordcount}/$totalproducts. saved {$result[0]} products.", null, $logfile);             //echo convert(memory_get_usage()) . " - processed {$recordcount}/$totalproducts. saved {$result[0]} products.\n";              $batcharrayids = array();          }      }       $batchmodel = mage::getmodel('dataflow/batch')->load($batchid);      try {          $batchmodel->beforefinish();      } catch (mage_core_exception $e) {          mage::log(convert(memory_get_usage()) . " - error: ". $e->getmessage(), null, $logfile);         //echo convert(memory_get_usage()) . " - error: ". $e->getmessage() . "\n";      } catch (exception $e) {          mage::log(convert(memory_get_usage()) . " - error: error occurred while finishing process. please refresh cache" . $e->getmessage(), null, $logfile);         //echo convert(memory_get_usage()) . " - error: error occurred while finishing process. please refresh cache" . $e->getmessage() . "\n";      }      $batchmodel->delete();      // output debugging info     foreach ($profile->getexceptions() $e) {          mage::log(convert(memory_get_usage()) . " - " . $e->getmessage(), null, $logfile);         //echo convert(memory_get_usage()) . " - " . $e->getmessage() . "\n";      }      mage::log("import complete.", null, $logfile);     //echo "import complete.\n";  ?> 

batch_import_processor.php

<?php      $root       = '/your/path/to/magento/root/directory/';     $logfile    = 'magento_import.log';      require_once $root . 'app/mage.php';      set_time_limit(0);     ini_set('memory_limit', '128m');      ob_implicit_flush();      mage::app()->setcurrentstore(mage_core_model_app::admin_store_id);      $params             = $argv[1];     $paramsarray        = json_decode($params, true);     $batchid            = $paramsarray['batchid'];     $importids          = $paramsarray['ids'];      $saved              = 0;     $batchmodel         = mage::getmodel('dataflow/batch')->load($batchid);     $batchimportmodel   = $batchmodel->getbatchimportmodel();     $adapter            = mage::getmodel($batchmodel->getadapter());      $adapter->setbatchparams($batchmodel->getparams());      foreach ($importids $importid) {           $batchimportmodel->load($importid);          if (!$batchimportmodel->getid()) {              mage::log(convert(memory_get_usage()) . " - error: skip undefined row {$importid}", null, $logfile);             //echo convert(memory_get_usage()) . " - error: skip undefined row {$importid}\n";             continue;          }          try {              $importdata = $batchimportmodel->getbatchdata();             $adapter->saverow($importdata);          } catch (exception $e) {              mage::log("exception : " . $e, null, $logfile);             //echo "exception : " . $e;             continue;          }          $saved ++;      }      if (method_exists($adapter, 'geteventprefix')) {          // event process rules relationships after import         mage::dispatchevent($adapter->geteventprefix() . '_finish_before', array(         'adapter' => $adapter         ));          // clear affected ids possible reuse         $adapter->clearaffectedentityids();      }      mage::log("total products import: " . $saved, null, $logfile);     echo $saved;  ?> 

it's not difficult add code can run multiple profiles in sequence if require. otherwise, sure profile id magento backend , set $profile_id variable desired value.

i've included calls magento log (mage::log) respective echo statements below if prefer route. edit needed. logs should saved in /magento/root/var/log/ directory. sure enable feature in magento backend going to:

  • system > configuration > advanced > developer > log settings

and setting enabled "yes" see below:

magento developer logging functionality

after running importer.php file, should have 2 log files in /magento/root/var/log/ directory:

  • system.log
  • magento_import.log

you may have exception.log file enabled.

this solution i've found work magento 1.9.1.0, go out andrey inspiration.

any thoughts or improvements welcome.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -