Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 6 years ago.
Improve this questionI have hooked the Quantity Selector to change the design and it works fine. But the problem is I can't set the max value
dynamically. If I have only 2 items in the stock I need to set max-value
as 2. Currently It's not working like that.
Following is the hook that I am using,
function woocommerce_quantity_input($data = null) {
global $product;
if (!$data) {
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'cw_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['input_value'] ) )
$defval = $defaults['input_value'];
else
$defval = 1;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else
$step = 1
?>
<div class="woo-single-selection">
<p>Quantity <?php //echo $product->get_stock_quantity(); ?></p>
<div>
<span class="woo-qty-num woo-quantity-input-number-decrement">-</span>
<input class="woo-quantity-input-number input-text qty text cw_qty" type="number" step="<?php echo $step ?>" value="<?php echo $defval ?>" min="<?php echo $min ?>" max="<?php echo $max ?>" name="<?php echo esc_attr( $defaults['input_name'] ) ?>" title="<?php echo _x( 'Qty', 'Product Description', 'woocommerce' ) ?>" pattern="[0-9]*" inputmode="numeric"><span class="woo-qty-num woo-quantity-input-number-increment">+</span>
</div>
</div>
<?php
}
How can set the maximum value as the available stock quantity value?
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 6 years ago.
Improve this questionI have hooked the Quantity Selector to change the design and it works fine. But the problem is I can't set the max value
dynamically. If I have only 2 items in the stock I need to set max-value
as 2. Currently It's not working like that.
Following is the hook that I am using,
function woocommerce_quantity_input($data = null) {
global $product;
if (!$data) {
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'cw_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['input_value'] ) )
$defval = $defaults['input_value'];
else
$defval = 1;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else
$step = 1
?>
<div class="woo-single-selection">
<p>Quantity <?php //echo $product->get_stock_quantity(); ?></p>
<div>
<span class="woo-qty-num woo-quantity-input-number-decrement">-</span>
<input class="woo-quantity-input-number input-text qty text cw_qty" type="number" step="<?php echo $step ?>" value="<?php echo $defval ?>" min="<?php echo $min ?>" max="<?php echo $max ?>" name="<?php echo esc_attr( $defaults['input_name'] ) ?>" title="<?php echo _x( 'Qty', 'Product Description', 'woocommerce' ) ?>" pattern="[0-9]*" inputmode="numeric"><span class="woo-qty-num woo-quantity-input-number-increment">+</span>
</div>
</div>
<?php
}
How can set the maximum value as the available stock quantity value?
function max_quantity_single( $max, $product ) {
$stock = get_post_meta( get_the_ID(), '_stock', true );
$max = $stock;
return $max;
}
add_filter( 'woocommerce_quantity_input_max','max_quantity_single', 10, 2 );
Add this hook in your functions.php or plugin file.
max
value equal to theavailable stock quantity value
– Ramesh Commented Dec 24, 2018 at 6:35