1

我正在将 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 中的全球分界线设置为一个认为这将是问题的时期,但事实并非如此。

知道什么可能导致这个问题吗?

4

1 回答 1

0

I have tested your code and it's working fine even if the field displays a coma instead of a period. When looking at the source code the value in the field is set with a period, but the displays shows that with a coma.

Below you can click on "Run code snippet" button and you will see:

<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>

So as you can see, the field value is 0.5, but the display is 0,5
Is just the normal behavior for this html5 <input type="number"> field and so this has nothing to do with WooCommerce.

In this related documentation about <input type="number"> "Allowing decimal values" section, the same thing happens. The value in the source is with a period, but displayed with a coma

In woocommerce you can control also everything in woocommerce_quantity_input_args unique filter hook:

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;
}

And you should need also (as in your code) this:

remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');

Code goes in function.php file of your active child theme (or active theme).

于 2018-01-22T16:31:13.883 回答