Re: unable to retreive a collection item from metafield (from collection page)

unable to retreive a collection item from metafield (from collection page)

toplocations
Tourist
8 0 4

I've attached a custom metafield to a subcollection, so i can attach it's parent collection to it like this:

Control Panels: (collection)

  • - simplex (sub)
  • - duplex (sub)

So on simplex, i have a metafield: custom.parentcollection

and this metafield type is a single 'Collection' (Control Panels)

toplocations_0-1701450878701.png

 

 

Now on this Simplex Collection Page, when i try to retrieve that parent's collection title, i'm unable to, seemingly no matter what i try

Even If i do a direct search, placing the parent collection's ID directly into liquid like this:

 

 

 

 

{% assign parent_collection = collections[11197939740] %}

 

 

 

 

when i go to reference that variable with {{ parent_collection }}, {{ parent_collection.title }}, or anything of the sort, i'm getting nothing and idk why 😕

 

I'm running on the latest theme version of Empire from Pixel Union.. Any help is appreciated, thanks in advance 🙂

Replies 2 (2)

MetafieldsGuru
Shopify Partner
160 32 102

Hi Toplocations,

 

Sasha here from Metafields Guru.

 

The answer to your question. From the looks of it, you are working with a collection reference metafield. The payload returned by Liquid for this type of metafield is a collection object. Unfortunately, you can't use an object to grab the data from the global object/array in that way. Consider retrieving the ID from the object and then using it to access the data.

 

A friendly suggestion. It would help if you opted for a more efficient approach. As you can see from the above part of my reply, you already have access to the parent collection object thanks to the reference metafield, so you don't need to retrieve it from the "collections". It is a redundant step. Also, the global variables like all_products, collections, etc. are extremely time- and resource-consuming (which is bad for your storefront) and you should avoid using them unless you have no other option.

 

Hope this helps!

Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension
toplocations
Tourist
8 0 4

so going from the metaobject, i thought i'd retrieved the ID of it's collection like this:

{% assign collection_id = collection_reference | split: '/' | last %}

but that wasn't working here

 

{% assign parent_collection = collections[collection_id] %}

 

 so that's why i pasted in the ID directly (thinking perhaps it wasn't retrieving it correctly), but even that still isn't working..

 

You're saying that using collections[collection_id] is redundant, but even if it try

 

{{ collection.metafields.custom.parentcollection.title  }} 

 

it doesn't load anything.. i'm just not understanding what i'm missing here 😕

Thanks for your time 🙂