Created
October 4, 2021 13:38
-
-
Save renventura/81d091548b34f9808b1ada5a5d308c8c to your computer and use it in GitHub Desktop.
MemberPress user cleanup when transaction fails
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 | |
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