Can’t access metafields from products in collections

{{currTest}} returns the value outside of the for but then {{ prod.metafields.prod_fields.test.value }} returns nothing. Hopefully somebody here can see what I’m doing wrong. Thanks

{% liquid

assign currTest = product.metafields.prod_fields.test.value

%}

{% paginate collections.all.products by 1000 %}

{% for prod in collections.all.products %}

{{ prod.metafields.prod_fields.test.value }}

{% endfor %}

{% endpaginate %}

Hi Franciely,

This issue could be due to the scope of your query. In your for loop, you are trying to access the test metafield for each product in the collection, but test metafield might not exist for all products. If the test metafield does not exist for a product, it will return null and it will appear as though nothing is returned.

To troubleshoot this issue, you can add conditionals to check if the test metafield exists for a product before trying to access its value. Here’s how you can do this:

{% liquid
  assign currTest = product.metafields.prod_fields.test.value
%}

{% paginate collections.all.products by 1000 %}
  {% for prod in collections.all.products %}
    {% if prod.metafields.prod_fields.test %}
      {{ prod.metafields.prod_fields.test.value }}
    {% else %}
      The test metafield does not exist for this product.
    {% endif %}
  {% endfor %}
{% endpaginate %}

This updated code will print “The test metafield does not exist for this product.” for every product that does not have a test metafield. This can help you see which products have the test metafield and which do not.

Test this out and let us know if you’re still seeing this issue.