The Add a value to a multi-value element rule

The Add a value to a multi-value element rule

As the name implies, this rule allows you to add a value to a multi-value element. Let’s first see what a multi-value element is.

:woman_mechanic: What is a multi-value element?

We describe the use of multi-value elements in detail in this post.

:woman_technologist: New rule Add a value to a multi-value element

In principle, this rule works similarly to the Overwrite rule. The difference is that it does not overwrite an existing value but adds one.

It even allows you to add the first value to an element if such an element is defined for the export (exists on the Elements page) but does not yet have a value. If it is not yet defined, you must first create the element on the Elements page.

  • Adding the first value does not make the element a multi-value element (see above for definitions).

:woman_office_worker: How the Add a value to a multi-value element rule works

The form for setting the rule looks like this:

In addition to the standard fields for naming the rule and selecting the product query, the rule has a field for specifying the element_path. Here, insert the new value and the field for the value itself. The new value can be an empty value, text, a variable of another element, or a combination of text and variable.

The element-path can be used for specifying which element to add the value to. However, you cannot use POSITION to specify which position to add the value to. It is always added to the next available position.

For example: if the selected element is empty, the rule adds the value to the first position. Or the selected element has one value, the rule adds the new value to the second position, etc.

It makes this rule different from other rules where you can use element-path to specify and edit a value at a specific position.

Adding a second value to an element makes it a multi-value element. With this rule, any element can be made a multi-value, including immersed elements.

:hammer_and_wrench: Examples of using the rule

Example x:

In the input feed, we have image URLs stored in this structure:

<images>
<image>
<url>url1</url>
<desc>description</desc>
</image>
<image>
<url>url2</url>
<desc>description</desc>
</image>
<image>
<url>url3</url>
<desc>description</desc>
</image>
</images>

In Mergado 2, such an entry looks like this (input values):

We need to achieve the following structure on the output:

<IMAGES>
<IMAGE>url1</IMAGE>
<IMAGE>url2</IMAGE>
<IMAGE>url3</IMAGE>
....
</IMAGES>

Method:

  1. The first step is to populate the first value of the IMAGES | IMAGE element. It can be done, for example, with the Overwrite rule.

Note that since the IMAGES | IMAGE element is multiple in our case when selecting the element where we want to insert the value, we need to specify its position using POSITION.

  1. In the second step, we add another (second) value using the Add value of multiple element rule.
    Note that we must use POSITION to specify where we want to take the URL value from. However, in the target multi-value element, we do not specify at which position we want to add the new value. The rule will add value to the next available position (in this case, the second position).
  2. In the third step, we add the next (third) value in the same way.
  3. Continue in this way for each additional value.

The result looks like this in Mergado (output data):

And the output feed itself: