Last active
December 14, 2023 09:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
$morder = new MemberOrder(); | |
// guess the order which triggered the current user level | |
// TODO use subs table to define if the subs is still active. | |
// 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