Can I transform a value during import (mark up prices, combine columns, set stock status)?
Published
· Last updated
Yes. On any field you choose either direct CSV mapping OR a PHP function, not both, because the function is the mapping: whatever it returns is stored for that row. The function receives the entire row, so you can read any column with $data[$h['Column Name']].
return function($data, $h) {
// add 30% markup to the retail price
$price = $data[$h['Retail Price']];
if (!is_numeric($price)) {
throw new InvalidArgumentException('Retail Price must be numeric.');
}
return (float) $price * 1.3;
};
Validate numeric input before doing arithmetic. A cast can discard a decimal fraction or trailing text; use a conversion that matches your data. To combine several image columns into one gallery field:
return function($data, $h) {
$images = [];
for ($i = 1; $i <= 6; $i++) {
$key = 'image_' . $i;
if (isset($h[$key]) && !empty($data[$h[$key]])) {
$images[] = trim($data[$h[$key]]);
}
}
return implode('|', $images);
};
Further reading
Browse the other questions.
Follow the visual import workflow.
Written for Super Speedy Imports 2.96.0.

Comments