我希望数量 Select 器值根据我们在Shipping>;Product Weight for Simple Products中设置的权重进行更改. 根据下图,当我们将产品的重量设置为0.5千克时,产品数量 Select 器从0.5开始,如果我们将其设置为1千克,则产品数量 Select 器从1开始.最后,当我们 for each 数字设置权重时,数量 Select 器应该基于我们定义的重量数字来启动. 我修改了一个代码,但它不适用于小于1的值.
/*Quantity Selector Based On Simple*/
function custom_quantity_selector_min_value( $min, $product ) {
$weight = $product->get_weight();
if ( $weight > 0 ) {
$min = $weight;
}
return $min;
}
add_filter( 'woocommerce_quantity_input_min', 'custom_quantity_selector_min_value', 10, 2 );
//Modify the quantity selector step value.
function custom_quantity_selector_step( $step, $product ) {
$weight = $product->get_weight();
if ( $weight > 0 ) {
$step = $weight;
}
return $step;
}
add_filter( 'woocommerce_quantity_input_step', 'custom_quantity_selector_step', 10, 2 );
//Update the quantity selector value dynamically.
function custom_quantity_selector_value( $input_value, $product ) {
$weight = $product->get_weight();
if ( $weight > 0 ) {
$input_value = $weight;
}
return $input_value;
}
add_filter( 'woocommerce_quantity_input_value', 'custom_quantity_selector_value', 10, 2 );