0

我在我的 WordPress 网站上使用 Memberpress 和 Memberpress Corporate,当会员在特定会员类型下注册或购买特定会员类型时,我正在尝试添加自定义功能。发生这种情况时,我需要获取公司帐户 ID 并对其进行处理。

我正在使用钩子 mepr-event-transaction-completed 因为这会触发重复和非重复交易,尽管我也尝试了 mepr-event-non-recurring-transaction-completed 以确保。

这是我的代码:

$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);
  }

当用户通过订阅注册此会员类型时,这没问题,我可以检索它,但是如果他们使用一次性非经常性交易进行注册,则公司帐户 ID 返回为 0,甚至虽然当我去检查数据库时,那里有一个公司帐户 ID。

对于非经常性交易,公司帐户 ID 是否在不同时间设置?

4

1 回答 1

0

好的,所以在与 Memberpress 交谈后,事实证明这只是没有在正确的时间设置。

我使用了一种解决方法:

  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
}
于 2022-02-24T17:03:52.317 回答