Liquid, JavaScript, themes, sales channels
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="{{ 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>
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
Save time & money ,Ask Questions The Smart Way
Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Buy Paul a ☕ Coffee for more answers or donate to eff.org
User | RANK |
---|---|
37 | |
28 | |
13 | |
12 | |
9 |
On our Shopify Expert Marketplace, you can find many trusted third party developers and fr...
By Arno Nov 27, 2023You've downloaded the Search & Discovery app from the Shopify App store, and as you're ...
By Skye Nov 8, 2023The year-end shopping season is just around the corner. Is a flash sale on your radar? Are...
By Jasonh Nov 6, 2023