Shopify themes, liquid, logos, and UX
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 -%}
<div class="offer-text-box {{ position }}"
style="
background-color: {{ hex_color }}{% unless opacity == 100 %}{{ opacity }}{% endunless %};
color: {{ text_color }};
font-size: {{ font_size }}px;
">
{{ offer_text }}
</div>
{% endif %}
Solved! Go to the solution
This is an accepted solution.
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 -%}
<div class="offer-text-box {{ position }}"
style="
background-color: {{ hex_color }}{% unless opacity == 100 %}{{ opacity }}{% endunless %};
color: {{ text_color }};
font-size: {{ font_size }}px;
">
{{ offer_text }}
</div>
{% 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
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
This is an accepted solution.
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 -%}
<div class="offer-text-box {{ position }}"
style="
background-color: {{ hex_color }}{% unless opacity == 100 %}{{ opacity }}{% endunless %};
color: {{ text_color }};
font-size: {{ font_size }}px;
">
{{ offer_text }}
</div>
{% 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
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']
Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025Discover opportunities to improve SEO with new guidance available from Shopify’s growth...
By Jacqui May 1, 2025