Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
I've set up a Product Variant reference metafield but when I try to use the metafield:
{{ product.metafields.bundle.product_1 }}
the output value is a GraphQL ID string:
gid://shopify/ProductVariant/517811666963
If I try to create a variant object from the metafield to access variant its attributes like id, title, product etc it doesn't output any data:
{%- assign variant = product.metafields.bundle.product_1 -%}
{{ variant.id }}
I'm guessing this is because the metafield is a string rather than an object. So how would I actually use a variant metafield to access the variant object's attribute?
Solved! Go to the solution
This is an accepted solution.
I managed to figure this out, you just need to append .value to the end of the metafied:
{{ product.metafields.bundle.product_1.value }}
Alternatively, is there any way to assign a variant from a variant ID similar to the all_product function eg
Instead of:
{% assign product = all_products[product-handle] %}
Something like:
{% assign variantId = product.metafields.bundle.product_1 | replace: 'gid://shopify/ProductVariant/'',''
{% assign variant = all_variants[variantId] %}
{{ variant.title }}
Or is there a way to use GraphQL objects with Liquid?
This is an accepted solution.
I managed to figure this out, you just need to append .value to the end of the metafied:
{{ product.metafields.bundle.product_1.value }}
I'm having the same issue, only getting the gid, however, adding .value did not work for me. Do you think it could be a theme issue? example
<p>{{product.metafields.custom.product_1.value.title}}</p>
this did not produce any output
but this <p>{{product.metafields.custom.product_1}}</p> produces
gid://shopify/Product/xxxxxxxxxxxx
Have you tried: {{product.metafields.custom.product_1.value}}
Hi Timd,
Thanks! I solved the issue. The product wasn't published to any sales channels (especially the store one). After that it worked.
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024