I am displaying Product Metafields on the products pages, but since I have different types of metafields for different types of products, I’d like to be able to automatically hide all metafields that have no value.
Also, inside a single type of product, there are cases where not all metafields will have value.
I would like to avoid having to add code for each product metafield I have created (19 so far) but instead encompass in a single code if that is possible.
Create metaobjects with metafield (you can use only one if you want) for each metafield you have now.
Then create mixed reference metafield (List) and select all your created metaobjects.
Then fill this metafield with proper metaobjects on product edit page.
Then you can use code like:
{%- assign structure = product.metafield.custom.test_all.value -%}
{%- if structure -%}
{%- for field in struncture -%}
{%- case field.system.type -%}
{%- when 'test1' -%}
{{ field.title }} - or any other metaobject value
{%- when 'test2' -%}
{{ field.title }} - or any other metaobject value
{%- endcase -%}
{%- endfor -%}
{%- endif -%}
You can put this in separate snippet and render in place you need.
Benefits - you can reuse same metaobjects (often happens), you can use same field type many times, easy to localize (you don’t need to localize each product, but just metaobject), easy to make filters with S&A native app and more…
So are you suggesting that instead of creating a metafield for each of the characteristics, I create a metaobject for each, which I then assemble in a single metafield? Would that allow to display only the characteristics that are filled?
How would I manage displaying and formatting the labels for the characteristics? I want to make sure to properly display the labels and another issue I have is that I would like for the label to adapt depending on the quantity setup in one the metafields.