Home / Archive / MemberPress: Hide the Cancel Subscription Option for the Set Number of Months
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Hide the Cancel Subscription Option for the Set Number of Months

The code snippet will hide the Cancel option for a user’s subscriptions under the Subscriptions tab on the Account page.

The sample code below will hide the Cancel link for the first 10 months of each 12-month subscription cycle. This will be applied to every billing cycle. As a result, users can cancel their subscriptions only during the 11th and 12th months of their subscriptions.

Code Preview
php
<?php
add_filter( 'mepr_custom_cancel_link', function( $link, $sub ) {
    // Get the subscription start time as a timestamp
    $subscription_start = strtotime( $sub->created_at );
    // Get the current time as a timestamp
    $current_time = time();
    
    // Calculate the number of months passed since the subscription started
    $months_passed = (int) ( ( date( 'Y', $current_time ) - date( 'Y', $subscription_start ) ) * 12 + ( date( 'm', $current_time ) - date( 'm', $subscription_start ) ) );
    
    // Calculate the current month in the 12-month cycle
    $current_month_in_cycle = $months_passed % 12;
    
    // Show cancel link only in the 11th month (10th index because the cycle starts from 0)
    if ( $current_month_in_cycle === 10 ) { //To change the number of months during which the Cancel option should be hidden, replace the number 10 with the desired number of months on this line.
        return $link; // Show the cancel link
    } else {
        return ''; // Hide the cancel link
    }
}, 10, 2 );

Comments

Add a Comment