我挂上了数量选择器来更改设计,效果很好。但问题是我不能设置max value
动态。如果库存中只有2件物品,我需要设置max-value
同于2。目前情况并非如此。
下面是我正在使用的钩子,
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
}
如何将最大值设置为可用库存数量值?
最合适的回答,由SO网友:keyur padaria 整理而成
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 );
在函数中添加此挂钩。php或插件文件。