Created
December 15, 2015 17:44
-
-
Save cartpauj/1bb00840a342ba22bfff to your computer and use it in GitHub Desktop.
MemberPress Subscription Actions
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 | |
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I edited expires_at field, but mepr-event-transaction-expired not work, can someone help me ?