Combining two arrays of products into one

I have two different product recommendation arrays. One is the the provided recommendations.products variable and the other is a metafield defined collection of products product.metafields.product_alternates.related_hardware.value. I have the following code:

{%- comment -%}
      Pull any manually defined related products as well as any auto generated products: 
    {%- endcomment -%}
    {% assign related_products = product.metafields.product_alternates.related_hardware.value %}
    {% if related_products != blank %}
      {% assign related_products = related_products | concat: recommendations.products %}
    {% else %}
      {% assign related_products = recommendations.products %}
    {% endif %}

However the resulting concat variable related_products only ever contains the metafield array’s contents (regardless of which is being appended to). My assumption as to what is going on here is that these are arrays of two different variable types and one array’s content is overriding the other rather than throwing some type of error. Does anyone know why the above code doesn’t work?

Hi @qdoor-dev ,

You can add condition and for metafield instead of concat it. For example code:

{% assign related_products = product.metafields.product_alternates.related_hardware.value %}
      
        {% if related_products != blank %}
          {%- for recommendation in related_products -%}
            - {% render 'card-product',
                  card_product: recommendation,
                  media_aspect_ratio: section.settings.image_ratio,
                  show_secondary_image: section.settings.show_secondary_image,
                  show_vendor: section.settings.show_vendor,
                  show_rating: section.settings.show_rating
                %}
            

          {% endfor %}
        {% endif %}
        {% for recommendation in recommendations.products %}
          - {% render 'card-product',
                card_product: recommendation,
                media_aspect_ratio: section.settings.image_ratio,
                show_secondary_image: section.settings.show_secondary_image,
                show_vendor: section.settings.show_vendor,
                show_rating: section.settings.show_rating
              %}
          
        {% endfor %}
      

Hope it helps!