我希望数量选择器值根据我们在“运输”>“产品重量”中为简单产品设置的重量进行更改。 如下图所示,当我们将产品的重量设置为0.5公斤时,产品数量选择器从0.5开始,如果我们设置为1公斤,则从1开始。最后当我们将重量设置为每个数字时,数量选择器应该根据我们定义的权重数字启动。 我修改了代码,但它不适用于小于 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 ); Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号