ShopSite Tip – Quantity Drop-down By Intervals

A few months ago we reviewed how you can use JavaScript to sell product in quantity increments, but that method only took effect on the cart.  What if you want to enforce the limits at the product level and use drop-downs so the customer can easily select their quantity?

In this post we’ll provide some simple PHP code which will allow you to automatically create a quantity drop-down field with the following features:

  1. Use the product’s “Minimum Quantity” value for a starting amount or a 1 if it’s blank
  2. Use the “Quantity On Hand” value for the max amount
  3. Use an extra product field to control the increments

The Code

This is a simple replace.  In your product template, find the code which currently displays the Quantity text box, such as:

<input type="text" name="[-- PRODUCT.RecordNumber --]:<span class=" size="2" />qnty" value="1">

..and replace it with this code:

[-- IF PRODUCT.Field2 --]<select name="[-- PRODUCT.RecordNumber --]:<span class=">qnty">
 <!--?php
 $qty_start = [-- PRODUCT.MinimumQuantity --]; IF ($qty_start==0){$qty_start=1;}
 $qty_end = [-- PRODUCT.QuantityOnHand --];
 $qty_step = [-- PRODUCT.Field2  --];
 foreach (range($qty_start, $qty_end, $qty_step) as $qty)
 echo "<option value=\"$qty\"-->$qty\n";
 ?>
 </select>
 [-- ELSE --]
 <input type="text" name="[-- PRODUCT.RecordNumber --]:qnty" size="2" value="1" />
 [-- END_IF --]

Values & Limitations

You will see the code starts off by checking an extra product field.  Select an extra product field you’re not using and change “Field2″ in both places in the code to the field# you are going to use.  This is the field you enter a number in to determine the increment steps, such as 25.

Since PHP code is used to build the steps your file will need to process PHP code.  This can be done by using a filename ending in .php (ex: myproduct.php) so the code can be executed.  Another option is to modify your .htaccess file to process PHP code in all .htm/.html files.

If you fill in a product’s “Minimum Quantity” field that value will be used for the first step, otherwise it will default to 1.

A Product’s Quantity On Hand value will be used for the maximum number the drop-down allows.  If you’re not tracking inventory then you can just replace “[– PRODUCT.QuantityOnHand –]” in the code with a number higher than what you expect a customer to order, such as 300.

  • TIP: Don’t set an extremely high end number with a low quantity increment as it will build a drop-down list with many rows.  For example a max of 10,000 with increments of 5 would create a 2000 line drop-down list.  This will be slow for a browser to load and a customer would never scroll through to get to the quantity they need.
  • NOTE: If you are using Advanced Options and tracking inventory, the [– PRODUCT.QuantityOnHand –] tag cannot be used.  There is no template variable to obtain the quantity on hand for a single option and since the customer selects the options on the page the quantity box cannot change its max limit based on the option selected.  Therefore you would want to hard-code a max limit like mentioned above and let the shopping cart enforce the inventory level when the customer adds the product to the cart.

Limits On The Cart

The shopping cart page can not display a drop-down for the quantity field, but there are a couple of options to still enforce the use of quantity increments.

1. If you never want people to change their quantity in the cart, and make them go back to a product’s details page to make changes, on the Commerce Setup > Order System > Shopping Cart screen you can enable the “Quantity can not be changed” field.

2. You can allow people to make changes in the cart but use javascript to enforce the increments.  Our blog post titled ShopSite Tip – Selling Products In Quantity Increments has all the details and sample code.

See It Work

To see how this can be implemented, try out this product in our test store:

http://shopsite-demo.lexiconn.com/qty-drop-down.php

That’s all there is to it – now with just a small change to a custom template you have drop-downs with quantity increments.

Looking for a web host that understands ecommerce and business hosting?
Check us out today!

Leave a Reply