How to add number in stock to variant label

Solved
alexmorris
Shopify Partner
36 2 17

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) 

Accepted Solution (1)
alexmorris
Shopify Partner
36 2 17

This is an accepted solution.

I managed to get it working with {{ product.variants[forloop.index0].inventory_quantity }} 

 

Thanks so much!

View solution in original post

Replies 5 (5)
farantariq93
Pathfinder
101 17 22

Hi Alex. Can you please show through a screenshot what you want?

If helpful then please Like and Accept Solution. Looking for bug fixing, website speed optimization or store maintenance , Hire me. Feel free to contact me in private message or on farantariq93@gmail.com.
Alan15
Shopify Partner
140 27 59

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

 

Need more help? Contact me here
alexmorris
Shopify Partner
36 2 17

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 

Alan15
Shopify Partner
140 27 59

It worked for me on the product page, but maybe your situation is different:

 

variants.png

Need more help? Contact me here
alexmorris
Shopify Partner
36 2 17

This is an accepted solution.

I managed to get it working with {{ product.variants[forloop.index0].inventory_quantity }} 

 

Thanks so much!