Have your say in Community Polls: What was/is your greatest motivation to start your own business?

How can I display a variant metafield only when it's not empty?

How can I display a variant metafield only when it's not empty?

Philip16
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?

example2.jpg

example.jpg

 

<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="{{ section.id }}-{{ option.position }}-{{ forloop.index0 }}" name="{{ option.name }}" value="{{ value | escape }}" form="{{ product_form_id }}" {% if option.selected_value == value %}checked{% endif %}>
<label for="{{ section.id }}-{{ 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 -%}

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

Reply 1 (1)

PaulNewton
Shopify Partner
7450 657 1565

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

https://shopify.dev/docs/api/liquid/objects/metafield 

https://shopify.dev/docs/api/liquid/objects/metafield#metafield-value 

https://shopify.dev/docs/api/liquid/basics#truthy-and-falsy 

 

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.

https://shopify.dev/docs/api/liquid/filters/translate 

 

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

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


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

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org