changing quantity field to dropdown on cart template

Hi there,

I wanted to change the the quantity field from a number input to a select menu on the cart/checkout template.

I edited the following from cart.inc.twig;


<div class="fc-cart__item__quantity">
{% if item.parent_code == '' or (item.parent_code != '' and item.quantity_min == 0) %}
{% if config.with_controls and checkout_sub_cancel == false %}
<input type="hidden" id="id{{ item.item_number }}" name="id{{ item.item_number }}" value="{{ item.id }}" />

<select class="fc-cart__item__quantity__input fc-form-control inputItem" name="quantity{{ item.item_number }}" id="quantity{{ item.item_number }}" data-fc-id="item-quantity-input" data-fc-item-id="{{ item.id }}">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

{% else %}
<p>{{ config.lang.cart_qty|raw }}: {{ item.quantity }}</p>
{% endif %}
{% else %}
<p>{{ config.lang.cart_qty|raw }}: {{ item.quantity }}</p>
{% endif %}
</div>


All works well, the prices update etc, however the dropdown menu always displays the value of option 1, even if i've selected option 4. I take it there must be some javascript involved - could you help point me in the right direction?
Comments
  • fc_adamfc_adam FoxyCart Team
    @saint_malo,

    Nice job! You're really close, you actually just need some additional Twig to pre-select the chosen value:
    <select class="fc-cart__item__quantity__input fc-form-control inputItem" name="quantity{{ item.item_number }}" id="quantity{{ item.item_number }}" data-fc-id="item-quantity-input" data-fc-item-id="{{ item.id }}">
    <option value="1" {% if item.quantity == 1 %}selected{% endif %}>1</option>
    <option value="2" {% if item.quantity == 2 %}selected{% endif %}>2</option>
    <option value="3" {% if item.quantity == 3 %}selected{% endif %}>3</option>
    <option value="4" {% if item.quantity == 4 %}selected{% endif %}>4</option>
    </select>
    That should get the correct value pre-selecting for you.
  • @fc_adam Thanks Adam - that's working perfectly.

Sign In or Register to comment.