Seleting a specific collection to pull metadata from

Topic summary

Issue: A product card badge should prioritize a “New In” collection badge, but Liquid can’t read the collection’s metafields. The product filtering for the new-in collection works; pulling the data does not.

Context: Code is placed in card-product.liquid for collection grid items. The collection object may not be directly available in this context without explicit assignment.

Key suggestions/updates:

  • Verify correct metafield targets exist on the collection and test output on a collection template.
  • Scope fix: Inside the loop that finds collection.handle == ‘new-in’, assign the found collection to a variable for use outside the loop (advisor provided an example and included assigning it, then using it after the loop).
  • Syntax fix: Advisor says the metafield access like collection.metafields[‘new-in’].collection[‘text_bg-color’] is incorrect; suggests using collection.metafields.new-in.text_bg-color.value.

Status/outcome:

  • OP will test the revised code and syntax after returning to their laptop.
  • No confirmed resolution yet; pending validation that scoping and metafield syntax changes allow pulling the “New In” collection metafields.

Notes: An image was shared to show metafield definitions and the collection URL (collections/new-in).

Summarized with AI on December 15. AI used: gpt-5.

I’m looking to have the new in badge override anything else defined for the sale badges I’ve added to my store,

the liquid correctly selects and isolates products that are in the new-in collection, but I can’t for the life of me get it to pull the data from the new-in (New In) collection metafields.

The rest works very well.

{% liquid 
  assign field_type = "collection"

  if card_product.metafields.product.offer_text != blank
    assign field_type = "card_product"
  endif

  for collection in card_product.collections
    if collection.handle == 'new-in'
      assign field_type = "new_in"
      break
    endif
  endfor

  if field_type == "new_in"
    assign hex_color = collection.metafields['new-in'].collection['text_bg-color']
    assign opacity = collection.metafields['new-in'].collection['text_bg_opacity']
    assign position = collection.metafields['new-in'].collection.text_position.value
    assign text_color = collection.metafields['new-in'].collection.text_color
    assign font_size = collection.metafields['new-in'].collection.font_size.value
    assign offer_text = collection.metafields['new-in'].collection.offer_text.value 
  elsif field_type == "collection"
    assign hex_color = collection.metafields.collection['text_bg-color']
    assign opacity = collection.metafields.collection['text_bg_opacity']
    assign position = collection.metafields.collection.text_position.value
    assign text_color = collection.metafields.collection.text_color
    assign font_size = collection.metafields.collection.font_size.value
    assign offer_text = collection.metafields.collection.offer_text.value
  elsif field_type == "card_product"
    assign hex_color = card_product.metafields.product.text_bg_color
    assign opacity = card_product.metafields.product.text_bg_opacity
    assign position = card_product.metafields.settings.text_position.value
    assign text_color = card_product.metafields.product.text_color
    assign font_size = card_product.metafields.product.font_size.value
    assign offer_text = card_product.metafields.product.offer_text.value 
  endif
%}
      
      {%- if card_product.metafields.product.offer_text != blank or collection.metafields.collection.offer_text -%}      
                 
               {{ offer_text }}
               

      {% endif %}

Hi @Mike-TOAV can you check you are targeting the correct metafield and also run test code to check if metafield is there on collection template

the definitions are definitely there due to the other tags working nicely and the collection url is collections/new-in

on which template/file you are adding above code?

directly to the card-product.liquid

file name which you are editing

try this

{% liquid 
  assign field_type = "collection"
 

  if card_product.metafields.product.offer_text != blank
    assign field_type = "card_product"
  endif

  for collection in card_product.collections
    if collection.handle == 'new-in'
      assign field_type = "new_in"
      assign collection = collection
      break
    endif
  endfor

  if field_type == "new_in"
    assign hex_color = collection.metafields['new-in'].collection['text_bg-color']
    assign opacity = collection.metafields['new-in'].collection['text_bg_opacity']
    assign position = collection.metafields['new-in'].collection.text_position.value
    assign text_color = collection.metafields['new-in'].collection.text_color
    assign font_size = collection.metafields['new-in'].collection.font_size.value
    assign offer_text = collection.metafields['new-in'].collection.offer_text.value 
  elsif field_type == "collection"
    assign hex_color = collection.metafields.collection['text_bg-color']
    assign opacity = collection.metafields.collection['text_bg_opacity']
    assign position = collection.metafields.collection.text_position.value
    assign text_color = collection.metafields.collection.text_color
    assign font_size = collection.metafields.collection.font_size.value
    assign offer_text = collection.metafields.collection.offer_text.value
  elsif field_type == "card_product"
    assign hex_color = card_product.metafields.product.text_bg_color
    assign opacity = card_product.metafields.product.text_bg_opacity
    assign position = card_product.metafields.settings.text_position.value
    assign text_color = card_product.metafields.product.text_color
    assign font_size = card_product.metafields.product.font_size.value
    assign offer_text = card_product.metafields.product.offer_text.value 
  endif
%}
      
      {%- if card_product.metafields.product.offer_text != blank or collection.metafields.collection.offer_text -%}      
                 
               {{ offer_text }}
               

      {% endif %}

also this syntax does not seems correct to get the metafield
collection.metafields[‘new-in’].collection[‘text_bg-color’]
it should be something like collection.metafields.new-in.text_bg-color.value

1 Like

Thank you, I’ll get my laptop out in a bit when I get home, just left work. and give that a try, supposedly enclosing certain CSS values in brackets is good for compatibility. That being said I was cheating a bit using openai and it suggested that.

its not cheating its a good use, you can give it a try and let me know, i think you are doing this on a collection grid product item so collection object is not available there, when you are finding it with for loop you will need to assign it to some variable to use outside loop.

you can ask openAi
is this correct shopify syntax? assign opacity = collection.metafields.collection[‘text_bg_opacity’]

1 Like