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.
Key Functionalities
Section titled “Key Functionalities”- 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.
Allowed PHP Functions
Section titled “Allowed PHP Functions”| Function Group | Functions |
|---|---|
| String Functions | strlen, strpos, strtolower, strtoupper, trim, substr, str_replace, explode, implode, htmlspecialchars, ucfirst, lcfirst |
| Hashing Functions | md5, sha |
| Array Functions | in_array, count |
| Math Functions | abs, round, ceil, floor, max, min, rand, sqrt, pow |
| Date/Time Functions | time, date, strtotime, mktime |
| Variable Handling Functions | isset, empty |
| JSON Functions | json_encode, json_decode |
| Multibyte String Functions | mb_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 |
Example Use Cases
Section titled “Example Use Cases”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_grossandlist_price_grossare0, set them asnet * 1.19. - Keywords: Replace
,with|. - Meta Title: Concatenate
meta_titleandnamecolumns.
General Format for the $row Script
Section titled “General Format for the $row Script”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.
1. Accessing Row Data
Section titled “1. Accessing Row Data”Each column in the row is accessed using its key in the $row array.
$manufacturerName = $row['manufacturer_name']; // Access the 'manufacturer_name' field2. Modifying Row Data
Section titled “2. Modifying Row Data”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']);3. Using Allowed PHP Functions
Section titled “3. Using Allowed PHP Functions”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']);4. Conditionally Modifying Data
Section titled “4. Conditionally Modifying Data”You can add logic to check conditions and modify values accordingly.
// Increase stock by 10 if it's greater than 0if (isset($row['stock']) && $row['stock'] > 0) { $row['stock'] += 10;}5. Performing Calculations
Section titled “5. Performing Calculations”Calculations can be performed on numerical data in the row.
// If price_gross is 0, set it as net * 1.19if (isset($row['price_gross']) && $row['price_gross'] == 0) { $row['price_gross'] = $row['net'] * 1.19;}6. Returning the Modified Row
Section titled “6. Returning the Modified Row”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.
Example of a Full Script
Section titled “Example of a Full Script”// 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 10if (isset($row['stock'])) { $row['stock'] += 10;}
// Set price_gross to net * 1.19 if price_gross is 0if (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'];Key Points
Section titled “Key Points”$rowis 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
$roware 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.