Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function user_saved( $user_id ) {
- // $product_id needs to be set to the membership level ID
- $txn = new MeprTransaction();
- $txn->user_id = $user_id;
- $txn->product_id = $product_id;
- $txn->txn_type = 'subscription_confirmation';
- $txn->gateway = 'manual';
- $txn->created_at = current_time( 'mysql' );
- $product = new MeprProduct($txn->product_id);
- // Can't use $txn->create_free_transaction( $txn ); since it forces a redirect, so copied the code from MeprTransaction
- if( $product->period_type != 'lifetime' ) { //A free recurring subscription? Nope - let's make it lifetime for free here folks
- $expires_at = MeprUtils::db_lifetime();
- } else {
- $product_expiration = $product->get_expires_at(strtotime($txn->created_at));
- if( is_null( $product_expiration ) ) {
- $expires_at = MeprUtils::db_lifetime();
- } else {
- $expires_at = MeprUtils::ts_to_mysql_date($product_expiration, 'Y-m-d 23:59:59');
- }
- }
- $txn->trans_num = MeprTransaction::generate_trans_num();
- $txn->status = 'pending';
- $txn->txn_type = 'payment';
- $txn->gateway = 'free';
- $txn->expires_at = $expires_at;
- // This will only work before maybe_cancel_old_sub is run
- $upgrade = $txn->is_upgrade();
- $downgrade = $txn->is_downgrade();
- $event_txn = $txn->maybe_cancel_old_sub();
- $txn->status = 'complete';
- $txn->store();
- $free_gateway = new MeprBaseStaticGateway('free', __('Free', 'memberpress'), __('Free', 'memberpress'));
- if($upgrade) {
- $free_gateway->upgraded_sub($txn, $event_txn);
- } elseif($downgrade) {
- $free_gateway->downgraded_sub($txn, $event_txn);
- }
- MeprUtils::send_signup_notices($txn);
- MeprEvent::record('transaction-completed', $txn); //Delete this if we use $free_gateway->send_transaction_receipt_notices later
- MeprEvent::record('non-recurring-transaction-completed', $txn); //Delete this if we use $free_gateway->send_transaction_receipt_notices later
- }
- add_action( 'pmxi_saved_post', 'user_saved' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement