0

I'm displaying a single add_to_cart_form and would like to have my select field for the quantity to reflect a number that I have in an "quantity" custom textfield. Currently, I have the following within my add_to_cart_form tags:

<select class="w-select" name="quantity">
    <option {selected} value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
 </select>
{if '{quantity}' == '' OR '{quantity}' == '1'}<input class="add btn w-button" type="submit" value="Add to Cart">{/if}
{if '{quantity}' == '0'}<p class="gray btn w-button">Sold Out!</p>{/if}

But, I would rather have the options reflect the amount of items available in the inventory. For example, for this item, I most commonly will have 5 available, but sometimes 3, sometimes 20. No matter how many there are, the price will be the same for each item, so using the multiple quantity option does not make sense.

It is entirely possible that this is super simple and I'm overthinking it, but thought I would ask.

Thanks in advance for your time!

0

I don't think there is any native solution for this. Only possible solution I can suggest is to enable PHP and run for loop for options:

<select class="w-select" name="quantity">
<?php for ( $i = 1; $i <= count({quantity}); $i++ ) { ?>
    <option value="<?= $i;?>" <?php if($i == 1) {echo 'selected';}?> ><?= $i;?></option>
    {!-- You can also break loop on maximum number you want for a sinlge item to be sold --}
    <?php if( $i == 20 ){ break; }?>
<?php } ?>
</select>

If you not want to enable PHP in template, You can create a small Plugin file too for handle this.

  • 1
    Great, thank you for your help! I'm not sure that I want to add PHP to my template, so I might go the lazy route. :) – fraserdev Apr 18 '18 at 15:10
  • What do you think to do then? By creating a plugin? – Mufi Apr 18 '18 at 15:21
  • I'm not experienced with creating a plug-in...and don't have time at the moment to learn how to create one as part of this project, unfortunately. – fraserdev Apr 18 '18 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.