How can I display metaobjects' content using liquid on a customer account page?

How can I display metaobjects' content using liquid on a customer account page?

jacopol
Tourist
18 0 3

Hi,
I'm having an issue with displaying metaobjects' content using liquid.

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 a 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. 

I already looked at posts here on the forum that deal with similar issues, and my attempts above come from the provide solutions, but they clearly don't work. 

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

Ciao!
Replies 3 (3)

NomtechSolution
Astronaut
1245 113 159

To access the values of nested metaobjects and their fields, you need to navigate through the nested structure using dot notation. Here's an example of how you can modify your Liquid code to correctly display the desired information:

{% assign licenses = customer.metafields.custom.license_new.value %}
{% for license in licenses %}
  {{ license.value.reference_product.value }} <br>
  {% for file in license.value.license_files.value %}
    <a href="{{ file.value.url }}">{{ file.value.filename }}</a> <br>
  {% endfor %}
{% endfor %}

In this code, we use license.value.reference_product.value to access the value of the reference_product field within the license metaobject, and file.value.url and file.value.filename to access the values of the url and filename fields within the license_files list.

jacopol
Tourist
18 0 3

Hi @NomtechSolution , thank you for your answer. 

My issue is that navigating through the nested structure is not working.
I skipped the files and link displaying in my examples for simplicity, since I'm not even able to retrieve the metaobject. 

I tried your example and it didn't work either. I noticed that the only difference in your code, except for the link and files display, is the first value inlicense.value.reference_product.value. Why is that needed?

Ciao!
jacopol
Tourist
18 0 3

I now managed to print something (ProductDrop, specifically), still not what I expect, but better than nothing.

The problem is that the used metaobject were still marked as Draft and not Active. 

Also, this example

 


@NomtechSolution wrote:

 

{% assign licenses = customer.metafields.custom.license_new.value %}
{% for license in licenses %}
  {{ license.value.reference_product.value }} <br>
  {% for file in license.value.license_files.value %}
    <a href="{{ file.value.url }}">{{ file.value.filename }}</a> <br>
  {% endfor %}
{% endfor %}

 

 


is wrong. 
The lines 
license.value.reference_product.value and license.value.license_files.value

should belicense.reference_product.value and license.license_files.value respectively.

 

Ciao!