First, on the dashboard from the side menu Connections and click on Transformers. This will take you to the overview page of the transformers. Click on the plus icon to create a new transformer configuration.
Fill in the basic information of the transformer:
- Name in this example we are going to pick an appropriate name for our transformer based on what it is going to do. So we are going to call it Prepare products. The Identifier will be automatically generated based on the Name field.
- Description in this field we can describe more in-depth what the transformer does, e.g.: Filter the configurable products and calculates the final price.
Now the basic information has been filled in we can start building the transformer configuration. For our prototype, we are going to select a data transformer. This is one of the easiest transformers to start with because it operates on the entire provided entity.
The filters on the data transformer are not going to be used in this example, because it affects the entire entity and not the specific node that we want.
During this example we will be using some test data to test the transformer configuration while we are working on it, on the right side of the form add the following data to the first editor:
"name": "Simple shirt",
"name": "Multi-color shirt",
"name": "Multi-color pants",
"name": "Set of cloting",
When this is done, the form should look like this:
Now to finalize this transformation, we are going to calculate the final price based on combining the base_price and the tax_price. To do this we first want to reset the keys of these nodes. Within the price, so we can apply another operator.
Add a new Value mapper transformer and use the Pattern accessor. This time we want to select all price nodes, so they can be operated on individually. We do not have to create a value mapper for each product, but can simply use a generic selection with the pattern accessor. The pattern for this would be: products.*.price. The * is used to select all children of the products node. A separate operation is performed for each child that is selected. So everything we do in this transformer will be applied to each price node individually.
Now add a new mapper to reset the keys in the price node. This will be the List: Get Values mapper again. It does not require any parameters to be configured for this operation.
Finally, we add another Operator mapper to calculate the prices. We only want a simple addition, so select the Addition operator. This will calculate the sum of all prices and set it on the price node.
If we run the transformer tester now, we can see the expected result.
Maintaining the original prices
If our external system actually wanted the array of data individually, plus the final price, we can make a few adjustments to our configuration to keep the original data intact.
Prior to the previous operation, we want to copy the price over to a separate node for calculation and then add it back into the price array.
First, add a new transformer and click on the arrow pointing upwards, to move it one position up in the chain of transformations. Select the Copy using a pattern transformer and fill in products.*.price as a pattern and for the replacement use products.$1.final_price. The $1 part in the replacement pattern, is used to select the value of the first * and use it as a variable in the replacement pattern. E.g.: products.0.price would be copied over to products.0.final_price, where $1 would be 0 for the first item in the array.
After this transformation is complete the price node will be copied (instead of moved) to the final_price node. Now we need to adjust our previous Value mapper transformer to select the final_price node instead of the price node.
In the accessor of the last Value mapper change the pattern to: products.*.final_price. This should be enough to calculate the price on the correct node.
Now add another Move using a pattern transformer to the chain and use the following configuration:
- Pattern: products.*.final_price
- Replacement: products.$1.price.final_price
This will set the calculated final_price in the price node and remove the original trace of the calculation.