Skip to content

Data Transformation

The new feature introduces a script editor window available at the first step of the import process. This editor allows customers to write and apply scripts to modify rows of imported files directly. In other words, it is possible to edit attribute values with the help of a custom script.

  • Script Input: Users can access the editor and write scripts to modify data using the variable $row, which represents the row being processed.
  • Function Whitelisting: For security purposes, only a limited set of PHP functions is allowed in the editor. These functions enable common string, array, and mathematical operations, ensuring users can perform common modifications without compromising system security.
Function GroupFunctions
String Functionsstrlen, strpos, strtolower, strtoupper, trim, substr, str_replace, explode, implode, htmlspecialchars, ucfirst, lcfirst
Hashing Functionsmd5, sha
Array Functionsin_array, count
Math Functionsabs, round, ceil, floor, max, min, rand, sqrt, pow
Date/Time Functionstime, date, strtotime, mktime
Variable Handling Functionsisset, empty
JSON Functionsjson_encode, json_decode
Multibyte String Functionsmb_ereg_replace, mb_ereg_replace_callback, mb_eregi_replace, mb_split, mb_str_pad, mb_str_split, mb_strcut, mb_strimwidth, mb_stripos, mb_stristr, mb_strlen, mb_strpos, mb_strrchr, mb_strrichr, mb_strripos, mb_strrpos, mb_strstr, mb_strtolower, mb_strtoupper, mb_substr, mb_substr_count

With the new feature, you can apply the following modifications to the imported data right in your Shopware 6 administration:

  • Manufacturer Name: Remove extra $ symbols from any values.
  • Options: Strip out spaces and replace ; with |.
  • Stock: Increase all values by 10.
  • Price Modification: If price_gross and list_price_gross are 0, set them as net * 1.19.
  • Keywords: Replace , with |.
  • Meta Title: Concatenate meta_title and name columns.

In the context of the feature allowing users to write scripts for row modification, the general format of the script involves processing each row of data through the variable $row, which is an associative array. Each key in the $row array represents a column or field from the imported data, and the corresponding value holds the actual data for that field.

Each column in the row is accessed using its key in the $row array.

$manufacturerName = $row['manufacturer_name']; // Access the 'manufacturer_name' field

You can modify the data by updating the corresponding key in the $row array.

// Remove any extra $ symbol from the manufacturer name
$row['manufacturer_name'] = str_replace('$', '', $row['manufacturer_name']);

The script can only use PHP functions from the predefined whitelist, which covers common operations like string manipulation, math, array handling, and date functions.

// Convert a string to lowercase
$row['options'] = strtolower($row['options']);

You can add logic to check conditions and modify values accordingly.

// Increase stock by 10 if it's greater than 0
if (isset($row['stock']) && $row['stock'] > 0) {
$row['stock'] += 10;
}

Calculations can be performed on numerical data in the row.

// If price_gross is 0, set it as net * 1.19
if (isset($row['price_gross']) && $row['price_gross'] == 0) {
$row['price_gross'] = $row['net'] * 1.19;
}

After performing all modifications, the script doesn’t need to explicitly return anything as the $row is passed by reference, meaning changes to the $row will automatically reflect in the system.

// Remove any extra $ symbols from manufacturer_name
$row['manufacturer_name'] = str_replace('$', '', $row['manufacturer_name']);
// Remove spaces and replace ';' with '|' in options
$row['options'] = str_replace(';', '|', trim($row['options']));
// Increase stock by 10
if (isset($row['stock'])) {
$row['stock'] += 10;
}
// Set price_gross to net * 1.19 if price_gross is 0
if (isset($row['price_gross']) && $row['price_gross'] == 0) {
$row['price_gross'] = $row['net'] * 1.19;
}
// Replace commas with '|' in keywords
$row['keywords'] = str_replace(',', '|', $row['keywords']);
// Join meta_title and name fields
$row['meta_title'] = $row['meta_title'] . ' ' . $row['name'];
  • $row is an associative array where each key represents a field/column of the row.
  • You can access and modify the row data using $row['column_name'] where column name is an actual column name from the imported file.
  • Only a whitelist of PHP functions is allowed, ensuring the script stays secure.
  • The modifications made to $row are automatically reflected in the import process.

By following this general format, you can write flexible and secure scripts for row modifications within the constraints of the whitelisted functions.