Solved

Is possible combine metafields with metaobjects in liquid?

DariusWS
Shopify Partner
84 1 21

Hi,

 

we have metafield for features: "first-feature,second-feature,third-feature"

 

It is possible to get metaobject value with something like this:

 

 

 

 

 

{% assign features = product.metafield.custom.features | split: ','%}
{% for feature in features %}
<span class="test123">{{ shop.metaobjects['main_features'][feature].text }}</span>
{% endfor %}

 

 

 

 

 

 

For me it returns empty...

Accepted Solution (1)

EcomGraduates
Shopify Partner
794 68 113

This is an accepted solution.

hello there  

Unfortunately, this is not possible in Liquid as metafields and metaobjects are two separate entities. Metafields are simply key-value pairs that can be associated with various Shopify resources, while metaobjects are more complex data structures that allow you to define custom data types and store more structured data.

To retrieve the value of a metaobject, you would need to use the shop.metaobjects object followed by the handle of the metaobject, then the name of the property you want to retrieve. For example:

 

 

 

{% assign feature_keys = product.metafield.custom.features | split: ',' %}
{% for feature_key in feature_keys %}
  {% assign feature = shop.metaobjects['main_features'][feature_key] %}
  {% if feature %}
    <span class="test123">{{ feature.text }}</span>
  {% endif %}
{% endfor %}

 

 

 

This code loops over each feature key in the features metafield, retrieves the corresponding metaobject using the shop.metaobjects object, and then outputs the text attribute of that metaobject if it exists.

 

 


 If this fixed your issue, likes and accepting as a solution are highly appreciated
|  Build an online presence with our custom-built Shopify Theme: EcomifyTheme
|  Check out our reviews: Trustpilot Reviews
|  We are Shopify Partners: EcomGraduates Shopify Partner



View solution in original post

Replies 3 (3)

EcomGraduates
Shopify Partner
794 68 113

This is an accepted solution.

hello there  

Unfortunately, this is not possible in Liquid as metafields and metaobjects are two separate entities. Metafields are simply key-value pairs that can be associated with various Shopify resources, while metaobjects are more complex data structures that allow you to define custom data types and store more structured data.

To retrieve the value of a metaobject, you would need to use the shop.metaobjects object followed by the handle of the metaobject, then the name of the property you want to retrieve. For example:

 

 

 

{% assign feature_keys = product.metafield.custom.features | split: ',' %}
{% for feature_key in feature_keys %}
  {% assign feature = shop.metaobjects['main_features'][feature_key] %}
  {% if feature %}
    <span class="test123">{{ feature.text }}</span>
  {% endif %}
{% endfor %}

 

 

 

This code loops over each feature key in the features metafield, retrieves the corresponding metaobject using the shop.metaobjects object, and then outputs the text attribute of that metaobject if it exists.

 

 


 If this fixed your issue, likes and accepting as a solution are highly appreciated
|  Build an online presence with our custom-built Shopify Theme: EcomifyTheme
|  Check out our reviews: Trustpilot Reviews
|  We are Shopify Partners: EcomGraduates Shopify Partner



jacopol
Tourist
18 0 3

Hi guys, I'm having a similar issues and don't understand what I'm doing wrong. 

I have a customer metafield containing list of metaobjects. The metafield has namespace custom and key license_new and it references a metaobject of type license which has 2 fields: 

1. reference_product: reference to the product. 
2. license_files: a list of generic files. 

I want to display the list of metaobjects, specifically a link to the reference_product and links to download the related license_files, in the customer account page. I'm attempting to do so using 

      {% assign licenses = customer.metafields.custom.license_new.value %}
      {% for license in licenses %}
        {{ license.reference_product.value }} <br>
      {% endfor %}


This doesn't seem to work and I don't understand why. 

I tried a similar approach with a simpler metafiled of key licenses that only contains a list of generic files. If I try to display the files IDs by doing 

      {% assign licenses = customer.metafields.custom.licenses.value %}
      {% for license in licenses %}
        {{ license.id }} <br>
      {% endfor %}


everything works as expected. 

Can anyone see what I'm doing wrong? Thank you 🙂 

Ciao!

enzoyuna2023
Shopify Partner
39 10 6
How about make simple like this? as long as the metafield definition referenced to metaObject
{%
assign feature = product.metafield.custom.features.value %}
 {% if feature!= blank %}
{{feature.text}}
{% endif %}
Available for hire : novayadi85[at]gmail.com
Chat on WhatsApp
Buy me coffee