Access Link Metafield using Custom Liquid on a Product Page

Access Link Metafield using Custom Liquid on a Product Page

zachatkinson
Shopify Partner
1 0 1

Hello everyone, and thank you for taking the time for looking at my issue.  I have a client who will be using Shopify as mostly a catalog, and we will be generating up to five different buy buttons that link directly to the product page for our featured dealers and distributors.  

I have created a custom metafield, chose its type as link, and created some dummy data in a part.  Then, I went to the theme customization panel (using a copy of Dawn) and added a custom liquid box to my product pages.  

 

When I try to run a loop to output the buttons, I seem to not be able to access any of the values inside the link object.  Basic error checking is showing the metafield exists, but I'm drawing blank on why I cannot seem to get access to the values themselves.  If anyone has any idea what I'm doing wrong a helping hand would be greatly appreciated!  Code below:

{% if product.metafields.custom.featured_dealer_links %}
{{product.metafields.custom.featured_dealer_links }} <!-- this works and shows the object -->
<h2>Test</h2>
{% assign featured_links = product.metafields.custom.featured_dealer_links %}
{% if featured_links %}
<p>Metafield exists!</p> <!-- metafield exists triggers -->
{% endif %}
{% for item in featured_links %}
{{ item.text | link_to: item.url, class: 'button button--full-width' }} <--does not display -->
{% endfor %}
{% endif %}

 

And some screenshots of the backend attached.

link-custom.jpg

link-custom2.jpg

Thanks again for taking the time to look at my issue.  I for the life of me cannot seem to access the values.  It appears link is just a fancy JSON object.... yet I'm drawing blank.  Thanks again for your time, I'm sure I've overlooked something simple but I'm new to Shopify dev.

Update: SOLVED

After messing with my code further, I stumbled upon the solution.  I hope this is helpful to anyone else using the link type in the future.

{% if product.metafields.custom.featured_dealer_links %}
 
  {% assign featured_links = product.metafields.custom.featured_dealer_links %}
  
    {% for item in featured_links.value %}
      <a href="{{ item.url}}" class="button button--full-width">{{ item.text }}</a>
    {% endfor %}
{% endif %}

My for loop turned out to be the issue. I had to add .value to featured_links! Once I did that, everything fell into place. I was able to access the values in the loop.  I had earlier tried to add .value to the actual link/button, but it was actually needed right in the for loop.

 

Replies 0 (0)