Show variant metafield only if not blank

Shopify Partner
2 0 0

I've created a variant metafield that I want to display only if there a value is entered for the variant.

Basically, I want to add "Best Value" to only specific variant option buttons.


My code is below.  The code in green is what I've added to the theme code.  I've only entered "Best Value" in the variant metafield for the "12" variant, but "Best Value" is showing in every variant.


I've attached screenshots of how it looks versus how I want it to look.

Can anyone help?




<div class="prod__option prod__option--button">
<div class="variant-picker__option-values inline-flex flex-wrap"> {%- for value in option.values -%} <div class="variant-picker__option" data-option-position="{{ option.position }}" data-option-type="{{ picker_type }}" data-value="{{- value | escape -}}">
<input type="radio" id="{{ }}-{{ option.position }}-{{ forloop.index0 }}" name="{{ }}" value="{{ value | escape }}" form="{{ product_form_id }}" {% if option.selected_value == value %}checked{% endif %}>
<label for="{{ }}-{{ option.position }}-{{ forloop.index0 }}" class="product-option-item sf-po__option-button border border-color-border flex items-center justify-center mr-2 mb-2 cursor-pointer variant-picker__option-button variant-picker__option-button--{{ picker_type }}">
{{- value -}}

{%- for variant in product.variants -%}

{%- if variant.metafields.custom.bestvalue != blank -%}

{{ variant.metafields.custom.bestvalue }}

{%- endif -%}

{%- endfor -%}

</div> {%- endfor -%} </div>

Reply 1 (1)
Shopify Partner
5931 537 1240

Metafields have a .value property , without it for many mf types you may be checking if the metafield exists at all with a blank comparison because truthiness in liquid.

{%- if variant.metafields.custom.bestvalue.value != blank -%} 


If this is a flag , and you do not need to control the output text in the metafield or per variant , then consider using a boolean metafield type,  and putting the output text either into a theme setting,  create a locale string or hardcoded in your code. 


Aside you've entered the wonderful world of the exhausting why- naming-things-is-hard because now to describe this feature its: the best value value for the best value product-variant .

Alternative namespace and key .merchandising.feature_text , .pricing.badge_text

Save time & money ,Ask Questions The Smart Way

Confused? Busy? Get the solution you need

Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to