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

每月20日至次月3日隐藏Woocommerce Subscriptions取消/重新激活按钮

代码调整方案

核心修改点

  • 新增站点时间范围判断逻辑,自动识别每月20日至次月3日的禁操作区间
  • 优化原有产品ID判断逻辑,仅当「订阅产品为ID1812」且「不在禁操作区间」时,才保留「取消」「重新激活」按钮,其余场景都隐藏对应按钮

调整后完整代码

/**
 * 隐藏订阅页面指定操作按钮,适配产品ID规则+月度时间段规则
 */
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
    // 1. 判断是否为ID1812的指定产品
    $is_my_product = false;
    if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
        foreach ( $subscription_items as $item_id => $item ) {
            $product = $item->get_product();
            if ( $product->get_id() == 1812 ) {
                $is_my_product = true;
                break;
            }
        }
    }

    // 2. 新增:判断是否处于每月20日~次月3日的禁操作时间段
    $current_day = (int)current_time('d'); // 适配WordPress后台设置的时区
    $is_forbidden_period = $current_day >= 20 || $current_day <= 3;

    // 仅当是1812产品 且 不在禁操作时间段时,才保留原有操作按钮
    if ( $is_my_product && !$is_forbidden_period ) return $actions;

    // 隐藏指定按钮
    foreach ( $actions as $action_key => $action ) {
        switch ( $action_key ) {
            // 不需要隐藏修改支付方式按钮的话可以注释掉下面这行
            // case 'change_payment_method':
            case 'reactivate': // 隐藏重新激活按钮
            case 'cancel': // 隐藏取消按钮
                unset( $actions[ $action_key ] );
                break;
            default: 
                // 不需要调试的话可以注释掉下面的日志输出
                // error_log( '-- $action = ' . print_r( $action, true ) );
                break;
        }
    }

    return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

注意事项

  • 时间判断使用WordPress官方current_time()函数,会自动适配后台设置的时区,不会出现时差问题
  • 原有产品规则完全保留,仅新增了时间维度的判断,不会影响之前的业务逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 15:48:01