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

WooCommerce Subscriptions开发:如何为特定产品保留取消订阅按钮

How to Keep Cancel Button Only for 1-Month Subscriptions in WooCommerce My Account

Hey there, let's adjust your existing code to add that exception for 1-month subscriptions. The core idea is to check what subscription product the user has, then only remove the cancel button if it's NOT a 1-month plan.

Here's the modified code with comments explaining each step:

<?php
/**
 * Plugin Name: Remove Subscription Action Buttons from My Account (With 1-Month Exception)
 * Description: Remove subscription action buttons from My Account page, except keep Cancel button for 1-month subscriptions.
 * Author: Brent Shepherd (modified for exception rule)
 * Version: 2.1
 */

function eg_remove_my_subscriptions_button( $actions, $subscription ) {
    // Initialize flag to check if this is a 1-month subscription
    $is_one_month_subscription = false;

    // Loop through items in the subscription to get the product details
    foreach ( $subscription->get_items() as $item ) {
        $product = $item->get_product();
        
        // Only check subscription-type products
        if ( $product->is_type( 'subscription' ) ) {
            // Get subscription interval and period (e.g., 1 + month = 1-month plan)
            $interval = $product->get_meta( '_subscription_period_interval' );
            $period = $product->get_meta( '_subscription_period' );
            
            // Check if it's exactly a 1-month subscription
            if ( absint( $interval ) === 1 && $period === 'month' ) {
                $is_one_month_subscription = true;
                break; // No need to check other items once we find a match
            }
        }
    }

    foreach ( $actions as $action_key => $action ) {
        switch ( $action_key ) {
            case 'change_payment_method':
                // Keep removing "Change Payment Method" button as before
                unset( $actions[ $action_key ] );
                break;
            case 'cancel':
                // Only remove Cancel button if this is NOT a 1-month subscription
                if ( ! $is_one_month_subscription ) {
                    unset( $actions[ $action_key ] );
                }
                break;
            // You can uncomment other cases below if you want to remove those buttons too
            // case 'change_address':
            //     unset( $actions[ $action_key ] );
            //     break;
            // case 'switch':
            //     unset( $actions[ $action_key ] );
            //     break;
            // case 'resubscribe':
            //     unset( $actions[ $action_key ] );
            //     break;
            // case 'pay':
            //     unset( $actions[ $action_key ] );
            //     break;
            // case 'reactivate':
            //     unset( $actions[ $action_key ] );
            //     break;
            default:
                // Optional: Log other actions for debugging if needed
                // error_log( '-- $action = ' . print_r( $action, true ) );
                break;
        }
    }
    return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );

Key Changes Explained:

  • Added a check to identify 1-month subscriptions by pulling the product's subscription interval and period from its metadata
  • For the cancel action key, we now only remove the button if the subscription is NOT a 1-month plan
  • All other original button removal logic (like change_payment_method) stays intact - you can adjust other cases by uncommenting them if needed
  • The loop through subscription items ensures we correctly identify the product even if the subscription has multiple items (though most subscriptions will have just one)

Just replace your current function with this modified version, then test it out with both 1-month and other subscription plans to confirm the behavior works as expected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 10:42:28