Getting a corporate account id from new Memberpress transaction (non subscription)

473 Views Asked by At

I'm using Memberpress and Memberpress Corporate on my WordPress site and I'm trying to add a custom function when a member signs up under a specific membership type or purchases a specific membership type. When this happens I need to grab the corporate account ID and do something with it.

I'm using the hook mepr-event-transaction-completed as this fires for both recurring and non-recurring transactions, though I also tried mepr-event-non-recurring-transaction-completed just to be sure.

This is my code:

$transaction = $event->get_data();
  $membership_type_ids = array(1, 2, 4);
  if (in_array($transaction->product_id, $membership_type_ids) && $transaction->txn_type == 'payment') {

    $org_id = $transaction->corporate_account_id;
     my_custom_function($org_id);
  }

When the user is signing up for this membership type with a subscription, this is no problem, I can retrieve this, however if they are signing up with a one-time non-recurring transaction, the corporate account id is returning as 0, even though when I go to check the database, there is a corporate account id there.

Does the corporate account id get set at a different time for non-recurring transactions?

1

There are 1 best solutions below

0
On BEST ANSWER

Okay, so after speaking to Memberpress, it turns out this just doesn't get set at the right time.

I used a workaround as so:

  if($transaction->corporate_account_id !== "0" && $transaction->corporate_account_id !== 0) {
    //some irrelevant code here about what to do if the corporate id actually works
  } else {
    write_log('sending cron to add new user due to corporate id returning as 0, please check in 2 minutes. tran_num = '.$transaction->trans_num);

    wp_schedule_single_event( strtotime("+2 minutes"), 'send_fix_for_zero_transaction', array($transaction),false );
    return;
  }

add_action( 'send_fix_for_zero_transaction', 'single_transaction_create_corporate' );

function single_transaction_create_corporate($transaction) {
  //NOTE: this function is only used it a one-time transaction is created in the backend to create a corporate membership, because there is a bug in memberpress that means the corporate_id isn't sent back by the event. This event will only fire if that is the case, otherwise this is handled by rc_setup_new_org. This should eventually be deprecated when Memberpress fix their issues.
  $trans_num = $transaction->trans_num;
  $full_trans = MeprTransaction::get_one_by_trans_num($trans_num); 
//do whatever you need to do with the transaction here, you have the number now
}