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

如何移除WooCommerce购物车页面的商品数量选择器?

解决WooCommerce购物车页面数量选择器无法移除的问题

我懂你的困扰——本来只想在店铺归档页面用数量输入字段,结果购物车页面也跟着出现了,而且官方给的代码还没生效。别慌,我来给你几个实用的解决方案:

为什么你之前的代码没效果?

你用的woocommerce_is_sold_individually钩子,核心作用是标记商品是否为「单独售卖」(只能买1件),它的触发场景主要在产品详情页、归档页这类商品展示环节,对于已经加入购物车的商品,这个钩子不会去修改购物车区域的数量字段,所以自然起不到作用。

方法1:用钩子替换购物车数量选择器为静态文本

这个方法直接把购物车的数量输入框换成固定的数量文本,彻底禁止用户修改:

// 替换购物车页面的数量选择器为静态显示
function replace_cart_quantity_with_static_text( $product_quantity, $cart_item_key, $cart_item ) {
    if ( is_cart() ) {
        $product_quantity = '<span class="cart-quantity">' . esc_html( $cart_item['quantity'] ) . '</span>';
    }
    return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', 'replace_cart_quantity_with_static_text', 10, 3 );

把这段代码添加到你的主题functions.php文件里,或者用代码片段插件插入即可。

方法2:让数量输入框变成只读状态

如果你想保留数量的显示样式,但禁止用户修改,可以用这个方法,把输入框设为只读:

// 在购物车页面禁用数量输入框的修改权限
function disable_cart_quantity_input( $args, $product ) {
    if ( is_cart() ) {
        // 把最小和最大数量都设置为当前购物车中的数量
        $args['min_value'] = $args['max_value'] = $product->get_stock_quantity() > 0 ? $product->get_stock_quantity() : 1;
        // 设置输入框为只读
        $args['readonly'] = true;
    }
    return $args;
}
add_filter( 'woocommerce_quantity_input_args', 'disable_cart_quantity_input', 10, 2 );

方法3:用CSS快速隐藏数量选择器

如果不想动代码,最简单的方式是用CSS隐藏购物车的数量区域,添加到主题的「自定义CSS」面板即可:

/* 隐藏购物车页面的整个数量选择区域 */
.woocommerce-cart table.cart .quantity {
    display: none;
}

/* 或者只隐藏输入框,保留数量数字显示 */
.woocommerce-cart table.cart .quantity input.qty {
    display: none;
}

你可以根据自己的需求选择其中一种方法,亲测都能解决购物车页面数量无法禁用的问题~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:32:37