Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michaelbeil/94c1938cc9d96ca0a71220e673e2210a to your computer and use it in GitHub Desktop.
Save michaelbeil/94c1938cc9d96ca0a71220e673e2210a to your computer and use it in GitHub Desktop.
When cancelling "all levels" in PMPro, do them one at a time so CONPD works.
<?php
/**
* This gist updates the "cancel all memberships" link on the cancel page
* to pass the level ids into the page instead of "all".
* This will ensure that all the levels are cancelled one by one.
* In addition to sending a separate email for each cancellation,
* this also ensures the $old_level parameter is sent to the
* pmpro_cancelMembershipLevel() function so Add Ons like the
* Cancel on Next Payment Date one continue to work.
*
* Add this into a custom plugin or as a code snippet:
* https://www.paidmembershipspro.com/how-to-add-code-to-wordpress/
*/
function my_pmpro_cancel_levels_one_by_one() {
global $current_user, $pmpro_pages, $wpdb;
// Make sure PMPro is active and we're on the cancel page.
if ( empty( $pmpro_pages ) || empty( $pmpro_pages['cancel'] ) ) {
return;
}
// Swap the levelstocancel REQUEST var so we cancel them all one by one
if ( ! empty( $_REQUEST['levelstocancel'] ) && $_REQUEST['levelstocancel'] === 'all' ) {
$old_level_ids = $wpdb->get_col("SELECT DISTINCT(membership_id) FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . esc_sql( $current_user->ID ) . "' AND status = 'active'");
$_REQUEST['levelstocancel'] = implode( ',', $old_level_ids );
$_GET['levelstocancel'] = $_REQUEST['levelstocancel'];
$_PUT['levelstocancel'] = $_REQUEST['levelstocancel'];
}
}
add_action( 'init', 'my_pmpro_cancel_levels_one_by_one', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment