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

如何在WooCommerce中仅为特定订阅产品启用手动续订?

针对特定WooCommerce订阅产品设置手动/自动续订的解决方案

全局基础设置

先完成WooCommerce全局配置:

  • 进入 WooCommerce > 设置 > 订阅
  • 将默认续订方式设为自动续订,让所有产品默认使用自动续订,后续仅对目标产品(永不过期会员)单独调整。

方法一:通过产品ID精准控制(适合单一目标产品)

将以下代码添加到主题的functions.php文件,或使用代码片段插件(如Code Snippets)执行:

add_filter( 'woocommerce_subscriptions_product_manual_renewal_enabled', 'custom_manual_renewal_for_target_product', 10, 2 );
function custom_manual_renewal_for_target_product( $enabled, $product ) {
    // 替换为你的永不过期会员产品ID
    $target_product_id = 123;
    
    // 匹配目标产品时启用手动续订
    if ( $product->get_id() === $target_product_id ) {
        return true;
    }
    
    // 其他产品沿用全局自动续订设置
    return $enabled;
}

方法二:通过自定义字段灵活控制(支持多产品)

如果需要批量或动态设置多个产品,可通过产品自定义字段实现:

  1. 打开目标产品编辑页面,右上角点击「选项」,勾选显示「自定义字段」
  2. 添加自定义字段:
    • 键(Name):enable_manual_renewal
    • 值(Value):yes
  3. 添加以下代码:
add_filter( 'woocommerce_subscriptions_product_manual_renewal_enabled', 'custom_manual_renewal_via_custom_field', 10, 2 );
function custom_manual_renewal_via_custom_field( $enabled, $product ) {
    // 获取产品自定义字段值
    $manual_renewal = get_post_meta( $product->get_id(), 'enable_manual_renewal', true );
    
    // 当自定义字段设为yes时启用手动续订
    if ( 'yes' === $manual_renewal ) {
        return true;
    }
    
    return $enabled;
}

针对变量订阅产品的适配

如果目标产品是变量订阅产品,需调整代码判断父产品ID:

add_filter( 'woocommerce_subscriptions_product_manual_renewal_enabled', 'custom_manual_renewal_for_variable_product', 10, 2 );
function custom_manual_renewal_for_variable_product( $enabled, $product ) {
    // 替换为变量产品的父ID
    $parent_product_id = 456;
    
    // 变体产品取父ID,普通产品取自身ID
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    
    if ( $product_id === $parent_product_id ) {
        return true;
    }
    
    return $enabled;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 04:01:15