Solved

Get collections by meta fields(I want to filter 'vendor' collections by a custom field 'region')

begginer1231
Shopify Partner
19 0 1

Hi,

 

I don't know how to retrieve collections with a particular meta field.

For example, I want to only get collections with a meta field 'region' equal to 'Alabama'.

{% for collection in collections %}
{% if collection.metafields.region.alabama %}
{% assign alabama_vendors = 'alabama'%}
{% endif %}
{% endfor %}
How can I conditionally retrieve collections by their meta field?
Is it even possible to access collections' meta fields?
 
Best
Accepted Solution (1)

gr_trading
Shopify Partner
1689 141 176

This is an accepted solution.

Hi @begginer1231 ,

 

Yes, You can.

 

Please follow below steps:

  1. From Shopify dashboard, go to Online store > Themes > Edit code
  2. Create custom metafield with name region
    hari1_prasad_1-1669292450096.png

     



  3. In the search box, search for the file named list-collections-template
  4. Search term {%- render 'collection-grid-item' and edit code with below script
{% unless collection.metafields.custom.region == "alabama" %}
   {%- render 'collection-grid-item', collection: collection, grid_item_width: grid_item_width -%}
{% endunless %}

hari1_prasad_0-1669292331355.png

 

Code is sample only code is multiple times in code, Please make sure to update it every where.

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee

View solution in original post

Replies 2 (2)

Jason
Shopify Expert
11190 225 2283

The metafields on a collection are certainly accessible via Liquid. I use them all the time.

However that loop you're doing will have a limit and if your shop has lots of collection it's possible (and likely) that the loop won't cover them all. I would also say it's not a very performant bit of code.

Looking at the code it's not clear what you're trying to achieve. I see a variable being set but given the simplicity I wonder if there's a better way to approach it. Feel free to post back with some more context.

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★

gr_trading
Shopify Partner
1689 141 176

This is an accepted solution.

Hi @begginer1231 ,

 

Yes, You can.

 

Please follow below steps:

  1. From Shopify dashboard, go to Online store > Themes > Edit code
  2. Create custom metafield with name region
    hari1_prasad_1-1669292450096.png

     



  3. In the search box, search for the file named list-collections-template
  4. Search term {%- render 'collection-grid-item' and edit code with below script
{% unless collection.metafields.custom.region == "alabama" %}
   {%- render 'collection-grid-item', collection: collection, grid_item_width: grid_item_width -%}
{% endunless %}

hari1_prasad_0-1669292331355.png

 

Code is sample only code is multiple times in code, Please make sure to update it every where.

For any custom development WhatsApp or connect at Email ID: gr.trading15@gmail.com for quick consultation. | Shopify Free codes
To support Buy Me a Coffee