Advertisement
verygoodplugins

Untitled

Aug 25th, 2019
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. function user_saved( $user_id ) {
  2.  
  3.     // $product_id needs to be set to the membership level ID
  4.  
  5.     $txn             = new MeprTransaction();
  6.     $txn->user_id    = $user_id;
  7.     $txn->product_id = $product_id;
  8.     $txn->txn_type   = 'subscription_confirmation';
  9.     $txn->gateway    = 'manual';
  10.     $txn->created_at = current_time( 'mysql' );
  11.  
  12.     $product = new MeprProduct($txn->product_id);
  13.  
  14.     // Can't use $txn->create_free_transaction( $txn ); since it forces a redirect, so copied the code from MeprTransaction
  15.  
  16.     if( $product->period_type != 'lifetime' ) { //A free recurring subscription? Nope - let's make it lifetime for free here folks
  17.  
  18.         $expires_at = MeprUtils::db_lifetime();
  19.  
  20.     } else {
  21.  
  22.         $product_expiration = $product->get_expires_at(strtotime($txn->created_at));
  23.  
  24.         if( is_null( $product_expiration ) ) {
  25.             $expires_at = MeprUtils::db_lifetime();
  26.         } else {
  27.             $expires_at = MeprUtils::ts_to_mysql_date($product_expiration, 'Y-m-d 23:59:59');
  28.         }
  29.     }
  30.  
  31.     $txn->trans_num  = MeprTransaction::generate_trans_num();
  32.     $txn->status     = 'pending';
  33.     $txn->txn_type   = 'payment';
  34.     $txn->gateway    = 'free';
  35.     $txn->expires_at = $expires_at;
  36.  
  37.     // This will only work before maybe_cancel_old_sub is run
  38.     $upgrade = $txn->is_upgrade();
  39.     $downgrade = $txn->is_downgrade();
  40.  
  41.     $event_txn = $txn->maybe_cancel_old_sub();
  42.     $txn->status = 'complete';
  43.     $txn->store();
  44.  
  45.     $free_gateway = new MeprBaseStaticGateway('free', __('Free', 'memberpress'), __('Free', 'memberpress'));
  46.  
  47.     if($upgrade) {
  48.  
  49.         $free_gateway->upgraded_sub($txn, $event_txn);
  50.  
  51.     } elseif($downgrade) {
  52.  
  53.         $free_gateway->downgraded_sub($txn, $event_txn);
  54.  
  55.     }
  56.  
  57.     MeprUtils::send_signup_notices($txn);
  58.     MeprEvent::record('transaction-completed', $txn); //Delete this if we use $free_gateway->send_transaction_receipt_notices later
  59.     MeprEvent::record('non-recurring-transaction-completed', $txn); //Delete this if we use $free_gateway->send_transaction_receipt_notices later
  60.  
  61. }
  62.  
  63. add_action( 'pmxi_saved_post', 'user_saved' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement