Walentin Widgren
Walentin Widgren

Reputation: 63

Set input field min and max for variable product in woocommerce

As our products always have a minimum quantity I have used the file quantity-input.php to set this. This works fine for single products but when a variable product is selected the input field resets the min value to 1 and empties the max value. How do I set a min and max value on variable products as well?

In quantity-input.php

// Volume varaibles
$v1 = get_field('v1') ?: '1';
<div class="quantity buttons_added">

    <?php echo $qty_start; ?><input id="inputNumber" type="number" step="<?php echo esc_attr( $step ); ?>" min="<?php echo $v1; ?>" max="9999" name="<?php echo esc_attr( $input_name ); ?>" value="<?php echo esc_attr( $input_value ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="input-text qty text" size="4" pattern="<?php echo isset( $pattern ) ? esc_attr( $pattern ) : ''; ?>" inputmode="<?php echo isset( $inputmode ) ? esc_attr( $inputmode ) : ''; ?>" /><?php echo $qty_end; ?>
</div>

The file is located in the global folder of woocommerce.

Upvotes: 1

Views: 3782

Answers (1)

Reigel Gallarde
Reigel Gallarde

Reputation: 65254

you should have not edited that file...

these hooks can achieve it. Just paste it in your theme's functions.php

add_filter( 'woocommerce_quantity_input_max', 'woocommerce_quantity_input_max' );
function woocommerce_quantity_input_max( $max ){
    $max = 10;
    return $max;
}

add_filter( 'woocommerce_quantity_input_min', 'woocommerce_quantity_input_min', 10, 2 );
function woocommerce_quantity_input_min( $min, $product ){
    if ( $product->get_id() == 89 ) {
          $min = 2;
    } else {
          $min = 3;
    }
    return $min;
}

you can even do some conditional statements on it.

Upvotes: 1

Related Questions