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

如何针对WooCommerce特定可变商品全局移除数量字段?

针对特定可变商品全局移除WooCommerce数量字段

你之前的代码会对所有商品生效,要只针对特定可变商品(包括其所有变体)移除数量字段,只需在过滤器函数里加入商品ID的判断逻辑即可。

修改后的代码如下:

add_filter( 'woocommerce_is_sold_individually', 'wc_remove_specific_variable_product_quantity_fields', 10, 2 );
function wc_remove_specific_variable_product_quantity_fields( $return, $product ) {
    // 替换为你需要移除数量字段的可变商品ID,支持多个ID
    $target_product_ids = array( 123 );

    // 处理变体商品:获取父商品ID,确保变体也被包含
    $current_product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // 判断当前商品是否属于目标可变商品
    if ( in_array( $current_product_id, $target_product_ids ) ) {
        return true;
    }

    // 其他商品保持原有设置
    return $return;
}

代码说明

  • 将$target_product_ids数组里的123替换为你的目标可变商品ID,多个ID用逗号分隔,比如array(123, 456)
  • 针对变体商品做了兼容:可变商品的子变体本身有独立ID,通过get_parent_id()获取父商品ID,确保所有变体都能隐藏数量字段
  • 返回true会让商品以"单独售卖"模式显示,自动隐藏所有位置的数量字段
  • 非目标商品不受影响,保持原有数量字段设置

使用方法

将这段代码添加到你的WordPress主题的functions.php文件中,或者通过自定义功能插件添加即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 07:57:34