Skip to content

Instantly share code, notes, and snippets.

@cartpauj
Created December 15, 2015 17:44
Show Gist options
  • Save cartpauj/1bb00840a342ba22bfff to your computer and use it in GitHub Desktop.
Save cartpauj/1bb00840a342ba22bfff to your computer and use it in GitHub Desktop.
MemberPress Subscription Actions
<?php
// 1) User purchases and payment is complete
function capture_completed_transaction($txn) {
//It's possible this could be a recurring transaction for a product the user is already subscribed to so probably use a user meta field described below
$user = new MeprUser($txn->user_id); //A MeprUser object
$membership = new MeprProduct($txn->product_id); //A MeprProduct object
$users_memberships = $user->active_product_subscriptions('ids'); //An array of membership CPT ID's
//Here you may want to grab a user meta field of your own creating and check to see if the user is already subscribed to this membership or not
//If not, then push them to your 3rd party application and update the user meta field
//If so, then do nothing
}
add_action('mepr-txn-status-complete', 'capture_completed_transaction');
//2) Transaction has expired (user cancelled, lapsed etc)
function catch_txn_expired($event) {
$txn = new MeprTransaction($event->evt_id); //evt_id should be the id of a transaction
$user = new MeprUser($txn->user_id);
$users_memberships = $user->active_product_subscriptions();
//Make sure they're not still subscribed to this membership somehow
if(!empty($users_memberships)) {
if(!in_array($txn->product_id, $users_memberships, false)) {
//They are actually expired so remove them from your third party app here
}
//else the user is still subscribed to this membership, so do nothing
}
else {
//The user has no memberships, so it's definitely expired
}
}
add_action('mepr-event-transaction-expired', 'catch_txn_expired');
//3a) User updates their email address (cannot change usernames in WP)
function user_changed_email($errors, $mepr_user) {
if(!empty($errors)) { return $errors; }
$new_email = stripslashes($_POST['user_email']);
if($mepr_user->user_email != $new_email) {
//Email has changed, so update your third party app here
}
}
add_filter('mepr-validate-account', 'user_changed_email', 10, 2); //Need to use this hook to get old and new emails
//3b) User updates password via MemberPress (if they do it via the WordPress login page you'll have to find another way to tie into that)
function catch_mepr_password_change() {
if(!isset($_REQUEST['action']) || $_REQUEST['action'] != 'updatepassword') { return; }
$new_pass = $_POST['mepr-new-password'];
$new_pass_confirm = $_POST['mepr-confirm-password'];
if(($new_pass == $new_pass_confirm) && !empty($new_pass)) {
//Push the password to your 3rd party app. $new_pass is plaintext password
}
}
add_action('init', 'catch_mepr_password_change', 3);
@lispirazione
Copy link

How can I add user roles when a user purchases a subscription?

@cartpauj
Copy link
Author

MemberPress does have a User Roles add-on that does that.

@lispirazione
Copy link

I don't understand why, but the Roles plugin doesn't work for me

@cartpauj
Copy link
Author

Are your member's transactions completing properly? I know the User Roles add-on triggers the role changes when the transaction is completed.

@llavillaccama
Copy link

Hello team,
There is a 'hook' that allows me to disable recurring payments based on the value of a perozalized field.
I have tried with 'mepr-validate-signup' but I have not been able to find where it is set if I have the recurring payment activated.

Regards

@sgnofx
Copy link

sgnofx commented Sep 29, 2021

How do I know if a user subscription has expired automatically

@phamcongsonit
Copy link

I edited expires_at field, but mepr-event-transaction-expired not work, can someone help me ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment