Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Metaobject Collection Field Output

Solved

Metaobject Collection Field Output

Bluflare
Shopify Partner
4 0 1

I have created a Metaobject called brands. It has a title, description, image and a collection reference. There is a screenshot of the Metaobject below. 

 

Screenshot 2024-11-15 at 22.57.36.png

 

 

I have included the current code I'm using below. Essentially I'm trying to output the collection link for the selected collection in the Metaobject entry to use for the a href. However all I seem to be able to output is the collection ID using the code I have or just gid://shopify/Collection/628469498189 for example using {{ brand.collection }}. 

I need to be able to output the collection handle for the selected collection, not the id. Is this possible? Nobody in Shopify support seems to be able to help. 

 

<section class="brands-list">
  <div class="brands-grid">
    {% for brand in shop.metaobjects.brands.values %}
      <div class="brand-item">
        {% if brand.collection %}
          {% assign collection_handle = brand.collection | split: '/' | last %}
          <a href="/collections/{{ collection_handle }}" class="brand-link">
        {% endif %}
          <img 
            src="{{ brand.logo | image_url: width: 400 }}" 
            alt="{{ brand.title }}" 
            class="brand-logo">
        {% if brand.collection %}
          </a>
        {% endif %}
        <h3 class="brand-title">{{ brand.title }}</h3>
      </div>
    {% endfor %}
  </div>
</section>

Many Thanks, 

 

Roo

Accepted Solution (1)

tim
Shopify Partner
3911 394 1435

This is an accepted solution.

To get what you need you should use .value -- this way you will get collection object from the collection field.

{{ brand.name }} <br/>

{{ brand.collection.value.url }} <br/>

produces:

My brand
/collections/all

 

Technically, you should also use brand.name.value,  but for text line system stores just the text. However for collection it stores the id.

Using .value allows you to fetch corresponding collection object and then use it's properties.

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com

View solution in original post

Replies 2 (2)

tim
Shopify Partner
3911 394 1435

This is an accepted solution.

To get what you need you should use .value -- this way you will get collection object from the collection field.

{{ brand.name }} <br/>

{{ brand.collection.value.url }} <br/>

produces:

My brand
/collections/all

 

Technically, you should also use brand.name.value,  but for text line system stores just the text. However for collection it stores the id.

Using .value allows you to fetch corresponding collection object and then use it's properties.

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
Bluflare
Shopify Partner
4 0 1

Legend Tim, thanks for clearing that up for me. Many Thanks!