我正在使用Woocommerce进行项目,我希望人们能够订购具有十进制值的产品。
现在我用这段代码很容易解决这个问题:
add_filter('woocommerce_quantity_input_min', 'min_decimal');
function min_decimal($val) {
return 0.5;
}
add_filter('woocommerce_quantity_input_step', 'nsk_allow_decimal');
function nsk_allow_decimal($val) {
return 0.5;
}
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');
但是,由于某种原因,网站上的HTML输出带有逗号的值,而不是像这样这样的句点:
<input id="quantity_5a66016934e7b" class="input-text qty text" step="0,5" min="0,5" max="" name="quantity" value="1" title="Aantal" size="4" pattern="" inputmode="" type="number">
这显然不起作用,因为它使用逗号而不是句点。但是我不知道是什么原因造成的。我已经将我的全球分界线设置在woocommerce的时期,认为这将是问题所在,但事实并非如此。
知道是什么导致了这个问题吗?
我已经测试了您的代码,即使该字段显示昏迷而不是句点,它也能正常工作。 查看源代码时,字段中的值设置了一个句点,但显示显示的是昏迷。
您可以在下面单击"运行代码片段">按钮,您将看到:
<div class="quantity">
<label class="screen-reader-text" for="quantity_5a660e741bc2f">Quantity</label>
<input type="number" id="quantity_5a660e741bc2f" class="input-text qty text" step="0.5" min="0.5" max="506" name="quantity" value="0.5" title="Qty" size="4" pattern="[0-9.]*" inputmode="numeric">
</div>
如您所见,字段值为
0.5
,但显示0,5
...
只是这个html5<input type="number">
字段的正常行为,所以这与WooCommerce无关。
在此有关">允许十进制值"部分的相关文档中<input type="number">
也会发生同样的事情。源中的值带有句点,但以昏迷显示
在woocommerce中,您还可以控制woocommerce_quantity_input_args
独特的过滤器钩子中的所有内容:
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 20, 2 );
function custom_quantity_input_args( $args, $product ) {
$args['input_value'] = 0.5; // Default starting value (Optional)
$args['min_value'] = 0.5;
$args['step'] = 0.5;
$args['pattern'] = '[0-9.]*';
$args['inputmode'] = 'numeric';
return $args;
}
您还应该(如在您的代码中)以下内容:
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');
代码进入活动子主题(或活动主题)的函数.php文件。经过测试并工作。