Skip to content

Instantly share code, notes, and snippets.

@renventura
Created October 4, 2021 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renventura/81d091548b34f9808b1ada5a5d308c8c to your computer and use it in GitHub Desktop.
Save renventura/81d091548b34f9808b1ada5a5d308c8c to your computer and use it in GitHub Desktop.
MemberPress user cleanup when transaction fails
<?php
add_action( 'mepr-txn-status-failed', 'my_custom_cleanup_memberpress_txn_failed', 15 );
/**
* Runs a "cleanup" when a MemberPress transaction fails.
* This checks the transaction's user to see if they have any completed transactions.
* If the user only has incomplete transactions, it deletes the user.
*
* @param object $txn MeprTransaction object
*
* @return void
*/
function my_custom_cleanup_memberpress_txn_failed( $txn ) {
// User from the transaction
$mp_user = $txn->user();
// Count complete transactions and non-complete transactions
$user_incomplete_txns = $mp_user->transactions( '`status` != "complete"' );
$user_complete_txns = $mp_user->transactions( '`status` = "complete"' );
// User only has non-complete transactions, so delete the user
if ( empty( $user_complete_txns ) && ! empty( $user_incomplete_txns ) ) {
$mp_user->destroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment