You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在WordPress中用PHP为number类型输入框设置min和max值

给WooCommerce数字输入框设置min和max限制

你只需要在woocommerce_form_field的配置数组里,和'type' => 'number'同级添加'min' => 1000与'max' => 10000这两个参数即可,修改后的完整代码如下:

add_action( 'woocommerce_before_add_to_cart_button', 'genius_product_price_input', 9 );
  
function genius_product_price_input() {
   global $product;
   if ( 100 !== $product->get_id() ) return;
   woocommerce_form_field( 'set_price', array(
      'type' => 'number',
      'min' => 1000, // 新增最小数值限制
      'max' => 10000, // 新增最大数值限制
      'required' => true,
      'placeholder'  => 'Min. 1000, max. 10000',
      'label' => 'Label text',
   ));
}

设置完成后,前端的数字输入框会自动生效这两个限制,用户无法输入超出范围的数值,浏览器也会给出对应的输入提示。

内容的提问来源于stack exchange,提问作者stackdo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 16:29:50