You need to enable JavaScript to run this app. WooCommerce如何为特定产品分类设置0.5精度十进制数量步长
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce如何为特定产品分类设置0.5精度十进制数量步长

WooCommerce 特定产品分类小数步长实现指南

核心逻辑说明

WooCommerce 默认会限制购买数量为整数、步长固定为1,且仅支持全局配置步长,要实现指定分类下0.5精度步长,需要分三层调整:

  • 放开目标分类商品的小数数量输入限制
  • 前端数量输入框自动绑定0.5步长规则
  • 加购、购物车更新、结账全链路做步长合规校验,避免非合规数量提交

代码实现

所有代码统一添加到子主题的functions.php文件末尾,或通过代码片段插件引入,仅需修改最开头的配置项即可生效:

第一步:全局配置项(仅需在这里修改目标分类)

// 配置项:替换为需要生效规则的产品分类ID数组
$GLOBALS['custom_quantity_rule_cats'] = array( 12, 34 ); // 示例:12、34为分类ID

分类ID获取方式:在WordPress后台进入「产品 > 分类目录」,点击目标分类的编辑按钮,在浏览器地址栏中找到tag_ID=参数,其后的数字即为对应分类ID

第二步:调整前端数量输入框规则

// 调整数量输入框的步长、最小值配置
add_filter( 'woocommerce_quantity_input_args', 'custom_category_quantity_step_args', 10, 2 );
function custom_category_quantity_step_args( $args, $product ) {
    $target_cats = $GLOBALS['custom_quantity_rule_cats'];
    if ( has_term( $target_cats, 'product_cat', $product->get_id() ) ) {
        $args['step'] = 0.5; // 步长固定为0.5精度
        $args['min_value'] = 0.5; // 可按需调整最小购买量
        // $args['max_value'] = 20; // 可按需设置最大购买量
        $args['input_mode'] = 'decimal';
    }
    return $args;
}

第三步:放开小数数量转换限制

WooCommerce 默认会将输入的数量强制转为整数,这部分逻辑仅对目标分类放开浮点型支持,不影响其他商品:

// 放开小数数量转换限制
add_filter( 'woocommerce_stock_amount', 'allow_decimal_quantity', 10, 2 );
function allow_decimal_quantity( $quantity, $digit ) {
    $target_cats = $GLOBALS['custom_quantity_rule_cats'];
    // 处理产品页加购场景
    if ( isset( $_REQUEST['add-to-cart'] ) ) {
        $product_id = absint( $_REQUEST['add-to-cart'] );
        if ( has_term( $target_cats, 'product_cat', $product_id ) ) {
            return floatval( $quantity );
        }
    }
    // 处理购物车页面场景
    if ( class_exists( 'WooCommerce' ) && WC()->cart ) {
        foreach( WC()->cart->get_cart() as $cart_item ) {
            $product_id = $cart_item['product_id'];
            if ( has_term( $target_cats, 'product_cat', $product_id ) ) {
                return floatval( $quantity );
            }
        }
    }
    // 非目标分类保持原有整数逻辑
    return intval( $quantity );
}

第四步:全链路步长合规校验

前端输入框的步长仅做引导,必须增加后端校验,避免用户手动输入不符合规则的数值(如0.3、1.2这类非0.5倍数的数量)直接提交:

// 加购环节的步长校验
add_filter( 'woocommerce_add_to_cart_validation', 'validate_category_quantity_step', 10, 5 );
function validate_category_quantity_step( $passed, $product_id, $quantity ) {
    $target_cats = $GLOBALS['custom_quantity_rule_cats'];
    if ( has_term( $target_cats, 'product_cat', $product_id ) ) {
        // 校验逻辑:数量乘以2后必须为整数,即符合0.5步长要求
        if ( fmod( $quantity * 2, 1 ) !== 0 ) {
            wc_add_notice( '该商品购买数量需为0.5的整数倍,可填写如0.5、1、1.5这类数值', 'error' );
            $passed = false;
        }
    }
    return $passed;
}
// 购物车更新环节的步长校验
add_filter( 'woocommerce_update_cart_validation', 'validate_cart_update_quantity_step', 10, 4 );
function validate_cart_update_quantity_step( $passed, $cart_item_key, $cart_item, $quantity ) {
    $target_cats = $GLOBALS['custom_quantity_rule_cats'];
    $product_id = $cart_item['product_id'];
    if ( has_term( $target_cats, 'product_cat', $product_id ) ) {
        if ( fmod( $quantity * 2, 1 ) !== 0 ) {
            wc_add_notice( '购物车内该商品购买数量需为0.5的整数倍,可填写如0.5、1、1.5这类数值', 'error' );
            $passed = false;
        }
    }
    return $passed;
}

注意事项

  • 若目标分类商品启用了库存管理,需要同步调整库存计量逻辑,避免小数库存值导致库存计算异常
  • 结账页的总金额计算会自动适配小数数量,无需额外调整
  • 逻辑兼容简单商品、可变商品、分组商品等所有WooCommerce产品类型

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.18 06:15:00