Skip to content

Instantly share code, notes, and snippets.

@mircobabini
Last active January 16, 2025 17:45
Show Gist options
  • Select an option

  • Save mircobabini/10dbf809fb3c53cda7f84e4d7b268211 to your computer and use it in GitHub Desktop.

Select an option

Save mircobabini/10dbf809fb3c53cda7f84e4d7b268211 to your computer and use it in GitHub Desktop.
PMPro snippet to disable the "Cancel" button when the auto-renew option is turned off.
<?php
/**
* Remove "Cancel" button for cancelled subscriptions.
*
* The user is still able to cancel auto-renew, but can't completely cancel the membership.
*/
add_filter( 'pmpro_member_action_links', function ( $pmpro_member_action_links ) {
global $current_user;
// bail if cancel link has been removed by someone else
if ( ! isset( $pmpro_member_action_links['cancel'] ) ) {
return $pmpro_member_action_links;
}
// get current user level
$level = pmpro_getMembershipLevelForUser( $current_user->ID );
// bail if not a recurring level
if ( ! pmpro_isLevelRecurring( $level ) ) {
return $pmpro_member_action_links;
}
if ( class_exists( 'PMPro_Subscription' ) ) {
$subscription = PMPro_Subscription::get_subscription( [
'user_id' => $current_user->ID,
'membership_level_id' => $level->id
] );
$subscription_is_active = $subscription && 'active' === $subscription->get_status();
if ( ! $subscription_is_active ) {
unset( $pmpro_member_action_links['cancel'] );
}
} else {
// backward compat with older pmpro
$morder = new MemberOrder();
// guess the order which triggered the current user level
// atm there's no way to know if a refunded order is for an active sub at gateway.
// it's better to assume that only orders in status "success" are still active at gateway.
$morder->getLastMemberOrder( $current_user->ID, 'success', $level->id );
// consider paid recurring orders without an enddate
if ( ! empty( $morder->id ) && ! empty( $morder->subscription_transaction_id ) && ! empty( $morder->enddate ) ) {
unset( $pmpro_member_action_links['cancel'] );
}
}
return $pmpro_member_action_links;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment