Skip to content

Instantly share code, notes, and snippets.

@mircobabini
Last active December 14, 2023 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mircobabini/10dbf809fb3c53cda7f84e4d7b268211 to your computer and use it in GitHub Desktop.
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;
}
$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