简体   繁体   中英

Shopify: How can I show the current variant value only in liquid?

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!

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 :)

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>...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM