Referencing a field inside a metaobject via liquid

Solved

Referencing a field inside a metaobject via liquid

j_knupp
Shopify Partner
9 0 0

I need some help please on a metaobjects/metafield problem.

 

I need to reference a field inside a specific metaobject. The specific metaobject is set by selecting the appropriate metaobject entry (from a list of possible selections) in a Product Metafield.  Once there, I need to loop through all the "products" in the field.  I've got the looping part, it's getting to the values in the field in the metaobject that I'm struggling with.

 

Here's the details and my code.

 

Metaobject type/name: formats

 

Metaobject field name/type: formats.available_options

 

Product metafield name/type: selected_option


Here's the code I have so far:

{% for refer_product in (???metaobject field???).value %}

{% assign productType = refer_product.type %}
{% assign productHandle = refer_product.handle %}

<a href="https://shop.xxx.com/products/{{ productHandle }}" class="linked_badge">{{ productType }}</a>

{% endfor %}


I need to know how to reference the metaobject field (formats.available_options) that is in the metaobject (formats) that is set/selected in each individual product via the product metafield (selected_product).

 

I get that if the product metafield just had values, I could use product.metafields.custom.selected_product.value. However, for reasons I need to do this with the metaobject approach. I'm struggling with how to reference the values in the specific metaobject field.

 

I've tried using (metaobject.formats.available_options.value) but that doesn't work. I'm guessing I need something above my first line of code to set/reference/call the specific Metaobject somehow so I can then loop through the values of the field....???  Or, is there a way to just reference the metaobject field directly?

 

I'm sure I'm just missing something here as I'm relatively new to MetaObjects.

 

Thanks!

 

-Justin

Accepted Solution (1)

WalkYourStyle
Explorer
440 54 78

This is an accepted solution.

It should be something like this

{% assign selected_option = product.metafields.custom.selected_option.value %}
{% assign available_options = selected_option.available_options.value %}

View solution in original post

Replies 4 (4)

WalkYourStyle
Explorer
440 54 78

This is an accepted solution.

It should be something like this

{% assign selected_option = product.metafields.custom.selected_option.value %}
{% assign available_options = selected_option.available_options.value %}
j_knupp
Shopify Partner
9 0 0

Bingo!

 

That did it.  Thank you very much!

PaulNewton
Shopify Partner
6854 615 1447

Ah the starting point was the product itself and it's metafield and not starting from the shop metaobject type, good work.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


PaulNewton
Shopify Partner
6854 615 1447

{{ shop.metaobjects.type.handle }} so {{ shop.metaobjects.formats.available_options }}

 

Gonna have to go through a bit of stuff before the paring down can happen,

Review the metaobject docs with a comb https://shopify.dev/docs/apps/build/custom-data/metaobjects/work-with-metaobjects 

Metaobjects are still new, relatively for the platform,  and they are made to overly general system, that along with the poor docs makes the pedagogy clunky at best.

So they are even harder to discuss for troubleshooting arbitrary use case implementations.

 

Taking a step back to get to minimal code example.

Go ahead and make that standalone metafield definition for a products list and make sure it outputs how you expect it to output without the meta interference of thinking about metaobjects.

 

Then get the metaobject itself first , then get some output such as by using the JSON filter, then fill in the logic.

Because this line {% for refer_product in (???metaobject field???).value %} implies your trying to access some sub-part but you've completely omitted any surrounding context, which may or may not be {% for entry in shop.metaobjects.formats %}...

  or  {% for entry in shop.metaobjects.formats.available_options %}  ?..

which means the products access syntax may be {{ entry.selected_option }} or {% for product in entry.selected_option %}...  ; with or without a .value output property .

 

FYI: A fast REPL for testing liquid-code is to use a custom-liquid section on a bare page with most other features disabled to quickly test code in the theme-designer; optionally in an unpublished theme to avoid changing the live theme.

 


@j_knupp wrote:

Product metafield name/type: selected_option


I get what your saying but technically there is no such metafield type as "selected_option" , https://shopify.dev/apps/metafields/types 

Verbiage will trip up troubleshooting, and yes that is shopify's fault for naming metaobject concepts this way.

 


@j_knupp wrote:

I need to know how to reference the metaobject field (formats.available_options) that is in the metaobject (formats) that is set/selected in each individual product via the product metafield (selected_product).

"available options for a product" , metaobjects are in a weird space make sure a simpler metafield wont do the trick for the real goal: https://xyproblem.info

Metaobjects can be better suited to multiple objects(products) needing to share the same group of value-sets

, or many large permutations(entries) that would be unmanageable in the product admin itself, etc.

Meanwhile a metafield type of list.single_line_text_field could handle a simple list of properties, and have predefined values.

 

 

 

 

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org