User Tools

Site Tools


urapidflow:data_preprocess

Pre-process data before import

The example is simplified, without handling of errors for events like file open or write.

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");
urapidflow/data_preprocess.txt · Last modified: 2011/07/06 15:11 by unirgy