Dynamically update product quantity dropdown based on variant [Masonry theme]

New Member
2 0 0

 

Hello,

I am trying to get our shop to have a dropdown selection for purchase quantity that is restricted to the inventory quantity. Shop link here: https://witchvamp.com/ 

 

Here is the product-template.liquid I have managed to figure out:

<div class="{% if section.settings.enable_payment_button %}with-payment-button{% endif %}">
   {% if section.settings.enable_payment_button %}
      <div>
	 <select id="quantity" name="quantity">
	    {% assign dropdown_qty = product.selected_variant.inventory_quantity %}
		{% for i in (1..dropdown_qty) %} {% assign q = i | times: 1 %}
			<option value="{{ q }}"{% if products.product.quantity == q %} selected{% endif %}>{{ i }}</option>
		{% endfor %}
                          
	</select>

        </div>
           <button type="submit" name="add" class="add-to-cart button button--alt">{{ 'products.product.add_to_cart' | t }}</button>
           {{ form | payment_button }}

For some reason {% assign dropdown_qty = product.selected_variant.inventory_quantity %} only seems to work if I use {% assign dropdown_qty = product.selected_variant_or_first_available.inventory_quantity %} but this only makes the dropdown list available quantities of the first variant and will not update to the appropriate quantities for other variants after load.

 

I look forward to hearing back, and would be happy to answer any questions you may have! Thank you!

 

0 Likes
Highlighted
Shopify Partner
297 56 71

Hello @WitchVamp

                                       Where are the product variant change selection options on your product page? 

0 Likes
Highlighted
New Member
2 0 0

Hello @Wahab_Ahmad !

Not every product has a variant, but nearly all of the clothing does. Are you referring to the variant selection on the webpage or in the liquid code?

0 Likes