skip to Main Content

I’m creating a product feed using XML + Liquid and I’m trying to send color and size info in the format shown below:

Ex. <color>Blue</color>
<size>M</size>

The liquid code below successfully retrieves and outputs the option name but it displays all of its values instead of the current variant one.

Here is my current code for review:

{% for variant in product.variants %}

    {% for product_option in product.options_with_values %}
    <{{ product_option.name }}>{% for value in product_option.values %}{{ value }}{% endfor %}</{{ product_option.name }}>
    {% endfor %}

{% endfor %}

Here is an example of the output I am getting

Screenshot of the feed as it outputs the values

Any feedback would be greatly appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    Here is how achieved it:

    {% if product.options.size == 1 %} <{{ product.options.first }}>{{ variant.option1 }}</{{ product.options.first }}> % else %} <{{ product.options.first }}>{{ variant.option1 }}</{{ product.options.first }}> <{{ product.options[1] }}>{{ variant.option2 }}</{{ product.options[1] }}> {% endif %}

    Where the output is:

    <color>White</color> <size>S</size>

    If you have a simpler solution by all means :)


  2. I’m not sure if I follow you 100%, but from your example you should be doing this instead:

    {% for product_option in product.options_with_values %}
        {%- for value in product_option.values -%}
            <{{ product_option.name }}>{{value}}</{{ product_option.name }}>
        {%- endfor -%}
    {% endfor %}
    

    Where that output will be:

    <color>Blue</color><color>Red</color><color>Green</color>...<size>M</size><size>S</size>...

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search