Seleting a specific collection to pull metadata from

Solved

Seleting a specific collection to pull metadata from

Mike-TOAV
Excursionist
15 1 0

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 %}

 

 

 

 

Accepted Solution (1)
Ahsan_ANC
Shopify Partner
1410 254 328

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


Contact for any custom design? Skype : ahsananc
>Was your question answered? Mark it as an Accepted Solution!
Feel Free to Contact Me at my Email: anc2277@gmail.com


To enhance and customize Shopify search and filter, I use Boost Product Filter & Search

View solution in original post

Replies 8 (8)

Ahsan_ANC
Shopify Partner
1410 254 328

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 

Contact for any custom design? Skype : ahsananc
>Was your question answered? Mark it as an Accepted Solution!
Feel Free to Contact Me at my Email: anc2277@gmail.com


To enhance and customize Shopify search and filter, I use Boost Product Filter & Search

Mike-TOAV
Excursionist
15 1 0

the definitions are definitely there due to the other tags working nicely and the collection url is collections/new-inScreenshot 2024-10-28 164421.png 

Ahsan_ANC
Shopify Partner
1410 254 328

on which template/file you are adding above code?

Contact for any custom design? Skype : ahsananc
>Was your question answered? Mark it as an Accepted Solution!
Feel Free to Contact Me at my Email: anc2277@gmail.com


To enhance and customize Shopify search and filter, I use Boost Product Filter & Search
Mike-TOAV
Excursionist
15 1 0

directly to the card-product.liquid

Ahsan_ANC
Shopify Partner
1410 254 328

file name which you are editing

 

Contact for any custom design? Skype : ahsananc
>Was your question answered? Mark it as an Accepted Solution!
Feel Free to Contact Me at my Email: anc2277@gmail.com


To enhance and customize Shopify search and filter, I use Boost Product Filter & Search
Ahsan_ANC
Shopify Partner
1410 254 328

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


Contact for any custom design? Skype : ahsananc
>Was your question answered? Mark it as an Accepted Solution!
Feel Free to Contact Me at my Email: anc2277@gmail.com


To enhance and customize Shopify search and filter, I use Boost Product Filter & Search
Mike-TOAV
Excursionist
15 1 0

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.

Ahsan_ANC
Shopify Partner
1410 254 328

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']

Contact for any custom design? Skype : ahsananc
>Was your question answered? Mark it as an Accepted Solution!
Feel Free to Contact Me at my Email: anc2277@gmail.com


To enhance and customize Shopify search and filter, I use Boost Product Filter & Search