Shopify / Shopify/liquid

Feature: Get the value of product options by title

Open
#1,015 12 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature Request In Progress
Dominant language
Ruby
Stars
11.9k
Forks
1.5k
Avg merge
17h 55m
Merged PRs (30d)
3

Description

Some time ago, I was asked by Stan Kostadinov to create an issue here.

Feature:

  1. At the moment it is not possible to get a list of all the values per option. EG:
    Getting all the values for the option 'color' would have to be done like this:
{% for product_option in product.options_with_values %}
 {% if product_option == 'color' %}
  {% for value in product_option.values %}
   {{ value }}
  {% endfor %}
 {% endif %}
{% endfor %}

Would it be possible to have something like this instead?:

{% for value in product.options_with_values['color'] %}
   {{ value }}
{% endfor %}
  1. Get the value of a variant option by title. EG:
    On some products we have the following options: Size, Color, Length. Respectively, these would be variant.option 1, 2 and 3. However, on some other products we have the options: Size and Pattern (Again, variant.option 1 and 2).
    If we want to get the color option of a product variant, whether it's there or not, whether the store admin or whoever we build the theme for has size before color, or the other way around. As a developer, you cannot know for sure whether the option 'color' will always be 'variant.option2'.
    So, what do you do?
{% for product_option in product.options %}
 {% if product_option == 'color' or product_option == 'colour' %}
  {% assign has_color = true %}
  {% assign color_index = 'option' | append: forloop.index %}
 {% endif %}
{% endfor %}

{% if has_color %}
 {% for variant in product.variants %}
 {{  variant[color_index] }}
 {% endfor %}
{% endif %}

Could we use something like variant.option['color'] or be able to get the title as well, instead of only the value? (variant.option1.title , variant.option1.value)

This could save a lot of lines of code and unnecesarry looping through tons of products/variants.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing product.options_with_values, product.options, product.variants, and variant.option access patterns shown in the issue. Determine how title-based option lookup should behave for missing options and whether both option titles and values are required; completion would need an agreed API and corresponding tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.