I did not manage to get it working, but I`m not very advanced.
I`ve put it in the functions.php, changed the values to real acf fields (all of them are set to output values) – created the final “merged_values” field as a text field, but after publishing the result field stays empty.