3

I'm trying to change the default +/- quantity selector into a drop down menu with a given amount of number options (i.e. 1-10).

Anyone know how to accomplish this?

Let me know if you'd like me to post any of the code related to this.

Tomelower
  • 475
  • 3
  • 8
  • 17
  • 3
    [What have you tried](http://whathaveyoutried.com/)? – Joseph Silber Jan 04 '13 at 17:28
  • I honestly don't know where to begin, I've tried looking through all the files related to the selector but have no idea what to do. – Tomelower Jan 04 '13 at 18:29
  • As useful as I found the answers to what I was looking for in my research, this question shows no research which is what the up/down vote is for. So in respect of SO, I've had to down vote. It would be good to at least show a woocommerce hook that you have tried or something like that. I appreciate you've offered to post code, however, you may consider posting code straight away in future posts. Hope this helps with your future SO interactions. We want to help, but the rules are important. – Tisch Aug 07 '14 at 23:49

6 Answers6

3

I'd like to do this too. So far I found that the quantity markup is generated in woocommerce/templates/single-product/add-to-cart/quantity.php. You could make a copy of this file in a minimal mirror of the woocommerce/templates directory structure in your theme folder, e.g. in this case copy it to yourtheme/woocommerce/single-product/add-to-cart. There you can edit it without altering the plug-in and risk it being overwritten when the plug-in is updated.

ivvi
  • 5,120
  • 5
  • 36
  • 53
  • I found this to be quite complicated to do for variation products. From [this answer](http://stackoverflow.com/a/12290037/1257965) it seems the database needs to be queried for variation prices, and I guess the same is true for variation stock. Reading that I gave up for now, as I would need drop-down selectors for all types of products. – ivvi Jan 08 '13 at 18:55
  • One thing I did find is that the quantity values `data-min` and `data-max` in `single-product/add-to-cart/quantity.php` are overriden by a JavaScript. The only way I found to set these values was to use the `woocommerce_quantity_input_args` filter in `woocommerce-template.php`. – ivvi Jan 08 '13 at 19:03
  • There is no `quantity.php` file in `woocommerce/templates/single-product/add-to-cart/` – martin schwartz Mar 13 '18 at 18:11
3

There is a plugin that will do this for you called WooCommerce Advanced Product Quantities, it's free and will allow you to set Minimum, Maximum and Step values for all product quantity inputs. Set rules on a per product, per category/tag or site wide.

http://wordpress.org/plugins/woocommerce-incremental-product-quantities/

It also works with WooCommerce Thumbnail Input Quantities which will put those quantity boxes in all of your product thumbnail boxes.

http://wordpress.org/plugins/woocommerce-thumbnail-input-quantities/

Enjoy! Full disclosure, I'm the plugin author.

Tyler Wiest
  • 137
  • 1
  • 5
  • Hi! Thanks for plugin! Unfurtunatelly it is not working any more :( Only override feature for each post is working. Are you going to fix it in near future? – Inoy Jun 02 '16 at 16:50
2

I have not tried this, but I found this code http://bastutor.blogspot.ca/2014/01/woocommerce-change-input-quantity-to-dropdown.html

/* Change Product Quantity Input to Dropdown */
function woocommerce_quantity_input() {
 global $product;

 $defaults = array(
  'input_name' => 'quantity',
  'input_value' => '1',
  'max_value'  => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
  'min_value'  => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
  'step'   => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
  'style'   => apply_filters( 'woocommerce_quantity_style', 'float:left; margin-right:10px;', $product )
 );

 if (!empty($defaults['min_value']))
  $min = $defaults['min_value'];
  else $min = 1;

 if (!empty($defaults['max_value']))
  $max = $defaults['max_value'];
  else $max = 20;

 if (!empty($defaults['step']))
  $step = $defaults['step'];
  else $step = 1;

 $options = '';
 for($count = $min;$count <= $max;$count = $count+$step){
  $options .= '<option value="' . $count . '">' . $count . '</option>';
 }

 echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
Nickfmc
  • 369
  • 1
  • 8
  • I tried this code. It works perfectly. Just not sure why you linked two different places with the same snippet? One in answer one in comments. – Trevor Oct 29 '15 at 21:38
  • Actually, yes while this code "works" it changes all quantity buttons to be dropdowns. So on the cart page this causes issues (does not update to reflect quantity in cart). Unfortunately looks like best option might be to update template file as detailed here: https://www.vanpattenmedia.com/2014/code-snippet-woocommerce-quantity-select-field – Trevor Nov 01 '15 at 02:52
0

You need to override the template "quantity-input.php" to do so add a file with the name as "quantity-input.php" in your-theme/woocommerce/global/ folder then you can make the changes in that file, now wordpress will use your file to display quantity input html.

Domain
  • 11,562
  • 3
  • 23
  • 44
0

Paste this in your function.php file The dropdown list works both on the product page and in the shopping cart.

Image

function woocommerce_quantity_input($args = array(), $product = null, $echo = true) {
       if (is_null($product)) {
          $product = $GLOBALS['product'];
       }
    
       // Default values
       $defaults = array(
          'input_name' => 'quantity',
          'input_value' => '1',
          'max_value' => apply_filters('woocommerce_quantity_input_max', -1, $product),
          'min_value' => apply_filters('woocommerce_quantity_input_min', 0, $product),
          'step' => 1,
       );
    
       $args = apply_filters('woocommerce_quantity_input_args', wp_parse_args($args, $defaults), $product);
    
       $args['min_value'] = max($args['min_value'], 0);
       $args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : 10;
    
       if (
          '' !== $args['max_value'] && $args['max_value'] < $args['min_value']
       ) {
          $args['max_value'] = $args['min_value'];
       }
    
       $options = '';
    
       // Add loop
       for ($count = $args['min_value']; $count <= $args['max_value']; $count = $count + $args['step']) {
    
          // Cart item quantity defined?
          if ('' !== $args['input_value'] && $args['input_value'] >= 1 && $count == $args['input_value']) {
             $selected = 'selected';
          } else {
             $selected = '';
          }
    
          $options .= '<option value="' . $count . '"' . $selected . '>' . $count . '</option>';
       }
    
       $html = '<div><div class="quantity form-select-wrapper"><select class="form- select" name="' . $args['input_name'] . '">' . $options . '</select></div><!--/.form-select-wrapper --></div>';
    
       if ($echo) {
          echo $html;
       } else {
          return $html;
       }
    }
-1
Hi paste this in your function.php file
    
function woocommerce_quantity_input($data = null) {
    global $product;
    if (!$data) {
    $defaults = array(
    'input_name'   => 'quantity',
    'input_value'   => '1',
    'max_value'     => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
    'min_value'     => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
    'step'         => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
    'style'         => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
    );
    } else {
    $defaults = array(
    'input_name'   => $data['input_name'],
    'input_value'   => $data['input_value'],
    'step'         => apply_filters( 'cw_woocommerce_quantity_input_step', '1', $product ),
    'max_value'     => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
    'min_value'     => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),
    'style'         => apply_filters( 'cw_woocommerce_quantity_style', 'float:left;', $product )
    );
    }
    if ( ! empty( $defaults['min_value'] ) )
    $min = $defaults['min_value'];
    else $min = 1;
    if ( ! empty( $defaults['max_value'] ) )
    $max = $defaults['max_value'];
    else $max = 15;
    if ( ! empty( $defaults['step'] ) )
    $step = $defaults['step'];
    else $step = 1;
    $options = '';
    for ( $count = $min; $count <= $max; $count = $count+$step ) {
    $selected = $count === $defaults['input_value'] ? ' selected' : '';
    $options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
    }
    echo '<div class="cw_quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product Description', 'woocommerce' ) . '" class="cw_qty">' . $options . '</select></div>';
    }