This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
urapidflow:v3:data_preprocess [2016/11/01 21:13] jamby77 created |
urapidflow:v3:data_preprocess [2017/05/19 19:08] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Pre-process data before import ====== | ||
+ | The example is simplified, without handling of errors for events like file open or write. | ||
+ | <file php bootstrap.php> | ||
+ | <?php | ||
+ | // initialize Magento environment | ||
+ | |||
+ | try { | ||
+ | // if you move this file, adjust bootstrap.php path | ||
+ | require __DIR__ . '/ | ||
+ | } catch (\Exception $e) { | ||
+ | echo <<< | ||
+ | {$e-> | ||
+ | </ | ||
+ | HTML; | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | $params = $_SERVER; | ||
+ | $params[\Magento\Store\Model\StoreManager:: | ||
+ | $params[\Magento\Store\Model\Store:: | ||
+ | $bootstrap = \Magento\Framework\App\Bootstrap:: | ||
+ | |||
+ | /** @var \Magento\Framework\App\Http $app */ | ||
+ | $app = $bootstrap-> | ||
+ | |||
+ | // configure environment | ||
+ | $om = $bootstrap-> | ||
+ | $areaList = $om-> | ||
+ | $areaCode = $areaList-> | ||
+ | /** @var \Magento\Framework\App\State $state */ | ||
+ | $state = $om-> | ||
+ | $state-> | ||
+ | /** @var \Magento\Framework\ObjectManager\ConfigLoaderInterface $configLoader */ | ||
+ | $configLoader = $om-> | ||
+ | $om-> | ||
+ | // end initialize Magento environment | ||
+ | |||
+ | </ | ||
+ | |||
+ | <file php urapidflow.php> | ||
+ | <?php | ||
+ | // load Magento libraries | ||
+ | include __DIR__ . '/ | ||
+ | |||
+ | // open original file for reading | ||
+ | $fs = @fopen(' | ||
+ | if(!$fs){ | ||
+ | echo ' | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // open file to be imported for writing | ||
+ | $fd = @fopen(' | ||
+ | if (!$fd) { | ||
+ | echo ' | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // retrieve column names | ||
+ | $fieldColumns = fgetcsv($fs); | ||
+ | $first = true; | ||
+ | |||
+ | // iterate through file | ||
+ | while ($r = fgetcsv($fs)) { | ||
+ | // get a row as associated array | ||
+ | $row = array_combine($fieldColumns, | ||
+ | |||
+ | // perform your data modifications here | ||
+ | // change existing columns | ||
+ | $row[' | ||
+ | |||
+ | // or add new columns, | ||
+ | // make sure that the new columns are always available | ||
+ | // and order of columns is always the same | ||
+ | $row[' | ||
+ | |||
+ | // output header | ||
+ | if ($first) { | ||
+ | fputcsv($fd, | ||
+ | $first = false; | ||
+ | } | ||
+ | |||
+ | // write the product row | ||
+ | fputcsv($fd, | ||
+ | } | ||
+ | // close files | ||
+ | fclose($fd); | ||
+ | fclose($fs); | ||
+ | |||
+ | // run the profile, which should be associated with final import file | ||
+ | $om-> | ||
+ | </ |