Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hello!
I'm attempting to get variant information from an input field within a metaobject definition (Note: not a metafield definition). The data type for the input is Variant which presents a variant picker to the user. Everything up to that point works as expected. I also have the same setup for a Product input field. What's odd to me is that I can easily access all of the information for the Product value that gets returned with code like this:
{%- for entry in shop.metaobjects.store_content_schedule['2024-04-10']['cart_drawer_content'].value -%}
{%- if entry.product != blank -%}
{%- assign content_media = entry.product.value.featured_image -%}
{%- endif -%}
{%- assign img_width = 240 -%}
{%- assign content_media_src=content_media | image_url: width: img_width -%}
{%- assign content_media_alt_text = content_media.alt -%}
<img
class="content-img"
src="{{ content_media_src }}"
alt="{{ content_media_alt_text }}"
width="{{ img_width }}"
height="{{ img_width }}"
>
{%- endfor -%}
As expected, this consistently yields an image with all of this information filled in. If I try to do the same thing with the Variant input field however, everything is blank. The only thing I've been able to successfully return from the Variant input was a gid by doing something like this:
{%- if entry.variant != blank -%}
{{ entry.variant }}
{%- endif -%}
All of the sane and wacky attempts below return absolutely nothing:
{%- if entry.variant != blank -%}
{{ entry.variant.id }}
{{ entry.variant.value }}
{{ entry.variant.value.id }}
{{ entry.variant.values.id }}
{{ entry.variant.id.value }}
{{ entry.variant.id.values }}
{{ entry.variant.value.id.value }}
{{ entry.variant.values.id.value }}
{{ entry.variant.value.id.values }}
{{ entry.variant.values.id.values }}
{{ entry.variant.value.id.values }}
{{ entry.variant.values.id.values }}
{{ entry.variant.value.id.values }}
{{ entry.variant.values.id.values }}
{%- endif -%}
I've checked the docs (metaobject, data types, variant object) and can't seem to find an explanation as to why this might be. Does the Variant input also need a Product input for some reason? Meaning do I first need to grab a product using the Product input field and then using that information use the Variant input field to target that specific variant within the Product returned from the Product input?
Any help that can be offered would be greatly appreciated!
Solved! Go to the solution
This is an accepted solution.
Soooooo egg on my face. It turns out I was seeing this behavior because I hadn't noticed that the variant I was randomly selecting in during my testing belonged to a product that had actually been archived. So, as one might expect, if the variant you're attempting to get via liquid is archived it'll return the gid but nothing else.
This is an accepted solution.
Soooooo egg on my face. It turns out I was seeing this behavior because I hadn't noticed that the variant I was randomly selecting in during my testing belonged to a product that had actually been archived. So, as one might expect, if the variant you're attempting to get via liquid is archived it'll return the gid but nothing else.
Thanks for your reply. I already tried similar things, and your reply helps me and saves a lot of time and effort. I want to add an extra suggestion. use the `value` key for accessing the variant data.
{% for variant in product.metafields.custom.variants.value %}
{
"actual_variant": {{ variant.variant.value.id }}
}
{% endfor %}
Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025