Introducing New Shopware 6 Feature: Data Transformation – Script Editor for Row Modification During Import

- Shopware

Modify attribure values with Data Trnaformation for Shopware 6 import
We are thrilled to introduce you to a new feature of our Improved Import, Export & Mass Actions App for Shopware 6Data 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. Let’s elaborate.

Key Functionalities

Below, we explain the key functionalities behind the Shopware 6 Data Transformation feature:

  • 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 include:

    • String operations: strlen, strpos, strtolower, strtoupper, trim, substr, str_replace, explode, implode, htmlspecialchars,
    • Array operations: in_array, count,
    • Mathematical operations: abs, round, ceil, floor, max, min, rand, sqrt, pow,
    • Date and time functions: time, date, strtotime, mktime,
    • Encoding functions: json_encode, json_decode,
    • Multibyte string functions: mb_ereg_replace, mb_ereg_replace_callback, mb_strtolower, mb_strtoupper, mb_substr, and more.

Full List of Allowed PHP Functions

Allowed PHP functions include:

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

Security Measures

The new feature is implemented in the safest possible way. It is associated with these security measures:

  • Whitelisting: Only the predefined set of functions is permitted, minimizing the risk of code vulnerabilities.
  • Malicious Input Detection: Scripts are monitored for potentially harmful patterns through regular expressions and specialized libraries.
  • Advanced Code Analysis: An Abstract Syntax Tree (AST) parser is employed to detect unsafe constructs in the code, ensuring that only safe operations are allowed.

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_gross and list_price_gross are 0, set them as net * 1.19.
  • Keywords: Replace , with |.
  • Meta Title: Concatenate meta_title and name columns.

Value to Customers

This feature empowers users to easily identify and fix problematic values during the import process, allowing for more flexible and accurate data management. By leveraging a secure scripting environment, customers can automate complex row adjustments without risking server security.

General Format for the $row Script

In the context of the feature allowing users to write scripts for row modification during the Shopware import, 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.

Structure

Here’s how the general format can be structured:

1. Accessing Row Data:

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

2. Modifying Row Data:

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

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.

Example using string manipulation:

4. Conditionally Modifying Data:

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

5. Performing Calculations:

Calculations can be performed on numerical data in the row.

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

Below, you can see an example of a full script necessary to modify data during Shopware 6 import:

Key Points

  • $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.