User Tools

Site Tools


urapidflow:data_preprocess

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
urapidflow:data_preprocess [2010/10/14 19:28]
unirgy
urapidflow:data_preprocess [2011/07/06 19:11] (current)
unirgy
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 urapidflow.php>​
 +<?php
 +
 +// load Magento libraries
 +include_once '​app/​Mage.php';​
 +Mage::​app('​admin'​)->​setCurrentStore(0);​
 +
 +// open original file for reading
 +$fs = fopen('​var/​urapidflow/​import/​source_file.csv',​ '​r'​);​
 +
 +// open file to be imported for writing
 +$fd = fopen('​var/​urapidflow/​import/​products.csv',​ '​w'​);​
 +
 +// 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,​ $r);
 +    ​
 +    // perform your data modifications here
 +    // change existing columns
 +    $row['​price'​] *= 1.2;
 +    ​
 +    // or add new columns, ​
 +    // make sure that the new columns are always available
 +    // and order of columns is always the same
 +    $row['​new_attribute'​] = '​Static value';​
 +    ​
 +    // output header
 +    if ($first) {
 +        fputcsv($fd,​ array_keys($row));​
 +        $first = false;
 +    }
 +    ​
 +    // write the product row
 +    fputcsv($fd,​ $row);
 +}
 +// close files
 +fclose($fd);​
 +fclose($fs);​
 +
 +// run the profile, which should be associated with final import file
 +Mage::​helper('​urapidflow'​)->​run("​Your Import Profile"​);​
 +</​file>​
urapidflow/data_preprocess.txt · Last modified: 2011/07/06 19:11 by unirgy