Shopify themes, liquid, logos, and UX
I want to add the number of available stock to each variant label in the Dawn theme.
So example S would look like S (10)
Solved! Go to the solution
This is an accepted solution.
I managed to get it working with {{ product.variants[forloop.index0].inventory_quantity }}
Thanks so much!
Hi Alex. Can you please show through a screenshot what you want?
Try in the main-product.liquid section file. Scroll down to the {%- when 'variant_picker' -%} part and update the code to include ({{ product.variants[index].inventory_quantity }}) after the {{value}} in the label and add a index variable:
{%- when 'variant_picker' -%}
{%- unless product.has_only_default_variant -%}
{%- if block.settings.picker_type == 'button' -%}
<variant-radios
class="no-js-hidden"
data-section="{{ section.id }}"
data-url="{{ product.url }}"
{{ block.shopify_attributes }}
>
{%- for option in product.options_with_values -%}
<fieldset class="js product-form__input">
<legend class="form__label">{{ option.name }}</legend>
{%- for value in option.values -%}
{% assign index = forloop.index0 %}
<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 }}">
{{ value }} ({{ product.variants[index].inventory_quantity }})
</label>
{%- endfor -%}
</fieldset>
{%- endfor -%}
Hey thanks Alan! unfortunately {{ product.variants[index].inventory_quantity }} is out of scope here so it returns empty. The way they've constructed the variant options is a proper pain in Dawn as you can't access variant data easily 😕
And if you stick a {% for variant in product.variants %} around it as it's already in a loop you get recursion... pain
It worked for me on the product page, but maybe your situation is different:
This is an accepted solution.
I managed to get it working with {{ product.variants[forloop.index0].inventory_quantity }}
Thanks so much!
User | RANK |
---|---|
227 | |
167 | |
66 | |
53 | |
53 |
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