在你的函数中试试这个。php:
add_filter( \'woocommerce_quantity_input_args\', \'control_product_qty_input\', 20, 2 );
function control_product_qty_input( $args, $product ) {
$settings = array(\'49\' => 3, \'48\' => 10); // for product id 49 3 products set
foreach ($settings as $k => $v) {
if($k === $product->get_id()){
$args[\'max_value\'] = $v;
$args[\'min_value\'] = $v;
}
}
return $args;
}
注意:当最小值和最大值相同时,Woocommerce会隐藏输入数字。
注意#2:此代码仅适用于单个产品视图。
编辑:您可以像这样在下拉列表中有多个值:(但我仍然对现有输入有一些问题,我不明白为什么。请与我一起尝试以下代码:)
add_filter( \'woocommerce_quantity_input_args\', \'control_product_qty_input\', 20, 2 );
function control_product_qty_input( $args, $product ) {
$settings = array(\'49\' => array(5, 8, 10, 13), \'48\' => array(2, 4));
$options = \'\';
$defaults = array(
\'input_name\' => \'quantity\',
\'input_value\' => \'1\',
\'max_value\' => apply_filters( \'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; margin-right:10px;\', $product )
);
$html = \'<div class="quantity_select" style="\' . $defaults[\'style\'] . \'"><select name="\' . esc_attr( $defaults[\'input_name\'] ) . \'" title="\' . _x( \'Qty\', \'Product quantity input tooltip\', \'woocommerce\' ) . \'" class="qty">\';
foreach ($settings as $k => $v) {
if($k === $product->get_id()){
foreach ($v as $v) {
$options .= \'<option value="\' . $v . \'">\' . $v . \'</option>\';
}
}
}
$html .= $options . \'</select></div>\';
echo $html;
}