How can I capture Memberpress user info after signup

I'm currently working on a website where users can purchase subscriptions using memberpress, I want to capture the user data after the signup process is completed and the payment transaction is also completed as well. If I add the member through dashboard manually I'm able to catch the user info as there is no payment gateway is involved, with this code

  function mepr_capture_new_member_added($event) {
    $user = $event-get_data();
    //mail('myemail', 'mp user added', 'new user added successfully');
  }
  add_action('mepr-event-member-added', 'mepr_capture_new_member_added');

As I'm new to WordPress development I don't know how I can access this data on my other page templates, that's why I'm sending an email to test if it works or not I was able to figure out the action hook for the transaction-completed event, but it doesn't seem to work properly. Here is the code

function mepr_capture_new_one_time_sub($event) {
    $transaction = $event-get_data();
    $user = $transaction-user();
    //mail('myemail', 'user purchased subscription', 'user-transaction-completed event captured '); }

add_action('mepr-event-non-recurring-transaction-completed', 'mepr_capture_new_one_time_sub');

I've read the complete memberpress documentation but the resources related to development are not what I'm looking for, they gave an option for webhooks and I can capture the data on zapier after user signup and payment transaction is completed but I need the data on my website so Zapier is not an option, they also provide the rest API but I want to capture the user information on 'signup and transaction complete' event and I don't think so that's possible with rest API, Please let me know how I can overcome this issue, any kind of help will be appreciated.

Topic development-strategy rest-api membership customization plugins Wordpress

Category Web


You can get the user by using the action mepr-txn-status-complete which fires on a successful transaction. If you need to, you can use a custom meta field to check whether or not this is the first time you have run this function against this user. This belongs in your theme's function file or in your custom plugin.

function capture_completed_transaction($txn) {
    $subscription = $txn->subscription();

    // Ensure the subscription is legit
    if($subscription === false) { return; }

    // Use any of the following objects to interact with as needed
    $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

    $current_user = new WP_User($txn->user_id);
    if ( ! $current_user->exists() ) {
        return;
    }

  }
add_action('mepr-txn-status-complete', 'capture_completed_transaction');

MemberPress' documentation is sadly lacking, after much frustration I found this helpful gist: MemberPress Subscription Actions


After searching for about 2 days I was able to figure out a way to capture the user after signup process is completed and the user's payment goes through the payment gateway, the way I'm doing it right now is when a signup process is completed user is redirected to thank-you page and a querystring containing subscr_id is passed on that page and I'm capturing that subscr_id and fetching the user from the database manually and its working perfectly fine, the process is seamless, and I can use the data on my other page templates as well.

You can simply get subscr_id from $_GET['subscr_id'] and use that to capture the user associated with this subscription from the table 'wp_mepr_subscriptions' and then use the user id to fetch the user detail from 'wp_usermeta' table of the wordpress, hope it helps someone else :)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.