liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Feature: Get the value of product options by title

Open Legic opened this issue 6 years ago • 12 comments

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.

Legic avatar Jul 18 '18 13:07 Legic

Hey, @Legic thanks for raising these. I know it's been a long time in the making but I'm currently working on resolving some of these issues.

I'll keep you posted.

shopmike avatar Oct 04 '19 00:10 shopmike

@Legic I'm happy to announce the issue you have described in #1 has now been implemented with a new options_by_name method on products.

Before

{% for product_option in product.options_with_values %}
 {% if product_option == 'color' %}
  {% for value in product_option.values %}
   {{ value }}
  {% endfor %}
 {% endif %}
{% endfor %}

After

{% for value in product.options_by_name.color.values %}
   {{ value }}
{% endfor %}

shopmike avatar Oct 04 '19 03:10 shopmike

@shopmike - Thank you for implementing this! Dumb question, is this available on Shopify now?

hasbrowow avatar Oct 04 '19 16:10 hasbrowow

Sure is, it will be available in the docs next week.

shopmike avatar Oct 05 '19 00:10 shopmike

Happy to announce that options_by_name is now case-insensitive. So you no longer need to check for color when in fact it is Color as the method will do it for you.

shopmike avatar Oct 11 '19 03:10 shopmike

https://help.shopify.com/en/themes/liquid/objects/product#product-options_by_name

shopmike avatar Oct 11 '19 03:10 shopmike

https://help.shopify.com/en/themes/liquid/objects/product#product-options_by_name

Let's gooooo! Thank you so much @shopmike !

However, there is still no way of getting the variant option value by for example 'color'.

Legic avatar Oct 17 '19 03:10 Legic

Please add: product.options_by_object or something like that, then output can be return:

[
	{
		"name": "color",  /* Default handle. */
		"value": "Color" /* Can be: Colour, Couleur, Farbe or Màu sắc... for translate */
	},
	{
		"name": "size",  /* Default handle. */
		"value": "Size" /* Can be: Taille, mărimea... for translate */
	},
	{
		"name": "material",  /* Default handle. */
		"value": "Material" /* Can be: Matérielle, Материјал... for translate */
	},
	{
		"name": "style",  /* Default handle. */
		"value": "Style" /* Can be: Stil, शैली.. for translate */
	}
]

By default product.options output return ['Couleur', 'Taille'] ==> We can't do anything with returns like this

And product.options_by_name or product.options_with_values seems to be the same

We, developers, need more than that but you never did.

notrealdev avatar Nov 04 '21 07:11 notrealdev

I would like to ask again for the feature requested in the last comment. Currently, options do not have a unique identifier, like an ID or a handle. The name field returns the translated display name of the option. When you want to add specific things to a specific option like we do, then you have to check for all languages, which does not make sense at all.

Would it be possible to introduce a feature like this, or is there a good workaround for this problem?

vaelu avatar Nov 11 '23 15:11 vaelu

Two years have passed and the problem is still here

notrealdev avatar Nov 11 '23 15:11 notrealdev

I would like to ask again for the feature requested in the last comment. Currently, options do not have a unique identifier, like an ID or a handle. The name field returns the translated display name of the option. When you want to add specific things to a specific option like we do, then you have to check for all languages, which does not make sense at all.

Would it be possible to introduce a feature like this, or is there a good workaround for this problem?

Did you find a workaround here?

Adrianjs42 avatar Jan 15 '24 22:01 Adrianjs42

I would like to ask again for the feature requested in the last comment. Currently, options do not have a unique identifier, like an ID or a handle. The name field returns the translated display name of the option. When you want to add specific things to a specific option like we do, then you have to check for all languages, which does not make sense at all.

Would it be possible to introduce a feature like this, or is there a good workaround for this problem?

Did you find a workaround here?

Unfortunately not.

vaelu avatar Jan 16 '24 07:01 vaelu