Skip to content

Instantly share code, notes, and snippets.

@cartpauj
Last active June 21, 2019 17:23
Show Gist options
  • Save cartpauj/bf2badc5d1e0732c4775fdcfa15edafc to your computer and use it in GitHub Desktop.
Save cartpauj/bf2badc5d1e0732c4775fdcfa15edafc to your computer and use it in GitHub Desktop.
UserPro hide or show profile based on subscription status
<?php
function mepr_sync_user_pro_visibility($txn, $status = false) {
global $userpro;
if(class_exists('MeprUser')) {
$user = new MeprUser($txn->user_id);
//Make sure it's a valid user still
if(!isset($user->ID) || !$user->ID) { return; }
$subs = $user->active_product_subscriptions();
if(!empty($subs)) {
$userpro->unblock_account($user->ID);
}
else {
$userpro->block_account($user->ID);
}
}
}
add_action('mepr-txn-store', 'mepr_sync_user_pro_visibility');
add_action('mepr-txn-expired', 'mepr_sync_user_pro_visibility', 11, 2);
@cartpauj
Copy link
Author

cartpauj commented Dec 4, 2017

This code can be pasted into a plugin like My Custom Functions.

@joshgarwood
Copy link

Hi Paul! Thank you so much for all of these snippets; they're really helping me understand how to tie-in to MemberPress events. I am trying to use this to enable/disable users in BuddyPress on my website. My question is, will the code above cover situations where the transaction/subscription is canceled or refunded? Or does it only work for expired transactions. I looked in the MemberPress documentation but couldn't find anything that was very helpful. Thanks for your time!

@cartpauj
Copy link
Author

mepr-txn-store is called anytime a transaction changes or is updated in MemberPress. So this should pretty well cover it.

@joshgarwood
Copy link

Thanks so much for the quick response!! I think this is actually working, but my problem is some misconfiguration with the paypal gateway. My transactions are stuck in a "pending" state when i view them in the Wordpress dashboard. But in PayPal the payment seems to have been processed successfully. I'm trying to investigate if there are any gateway issues now.

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