pmpro_insChangeMembershipLevel

Change the membership level.

Description

pmpro_insChangeMembershipLevel( $txn_id, &$morder ); 

We also update the membership order to include filtered valus.

Parameters (2)

0. $txn_id
The txn id.
1. $morder
The morder.

Usage

  1. if ( !function_exists( 'pmpro_insChangeMembershipLevel' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/services/twocheckout-ins.php'; 
  3.  
  4. // The txn id. 
  5. $txn_id = null; 
  6.  
  7. // The morder. 
  8. $morder = &$morder; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pmpro_insChangeMembershipLevel($txn_id, $morder); 
  12.  

Defined (1)

The function is defined in the following location(s).

/services/twocheckout-ins.php  
  1. function pmpro_insChangeMembershipLevel($txn_id, &$morder) 
  2. $recurring = pmpro_getParam( 'recurring', 'POST' ); 
  3.  
  4. //filter for level 
  5. $morder->membership_level = apply_filters("pmpro_inshandler_level", $morder->membership_level, $morder->user_id); 
  6.  
  7. //set the start date to current_time('mysql') but allow filters (documented in preheaders/checkout.php) 
  8. $startdate = apply_filters("pmpro_checkout_start_date", "'" . current_time('mysql') . "'", $morder->user_id, $morder->membership_level); 
  9.  
  10. //fix expiration date 
  11. if(!empty($morder->membership_level->expiration_number)) 
  12. $enddate = "'" . date_i18n("Y-m-d", strtotime("+ " . $morder->membership_level->expiration_number . " " . $morder->membership_level->expiration_period, current_time("timestamp"))) . "'"; 
  13. else 
  14. $enddate = "NULL"; 
  15.  
  16. //filter the enddate (documented in preheaders/checkout.php) 
  17. $enddate = apply_filters("pmpro_checkout_end_date", $enddate, $morder->user_id, $morder->membership_level, $startdate); 
  18.  
  19. //getdiscountcode 
  20. $morder->getDiscountCode(); 
  21. if(!empty($morder->discount_code)) 
  22. //update membership level 
  23. $morder->getMembershipLevel(true); 
  24. $discount_code_id = $morder->discount_code->id; 
  25. else 
  26. $discount_code_id = ""; 
  27.  
  28.  
  29.  
  30. //custom level to change user to 
  31. $custom_level = array( 
  32. 'user_id' => $morder->user_id,  
  33. 'membership_id' => $morder->membership_level->id,  
  34. 'code_id' => $discount_code_id,  
  35. 'initial_payment' => $morder->membership_level->initial_payment,  
  36. 'billing_amount' => $morder->membership_level->billing_amount,  
  37. 'cycle_number' => $morder->membership_level->cycle_number,  
  38. 'cycle_period' => $morder->membership_level->cycle_period,  
  39. 'billing_limit' => $morder->membership_level->billing_limit,  
  40. 'trial_amount' => $morder->membership_level->trial_amount,  
  41. 'trial_limit' => $morder->membership_level->trial_limit,  
  42. 'startdate' => $startdate,  
  43. 'enddate' => $enddate); 
  44.  
  45. global $pmpro_error; 
  46. if(!empty($pmpro_error)) 
  47. echo $pmpro_error; 
  48. inslog($pmpro_error); 
  49.  
  50. if( pmpro_changeMembershipLevel($custom_level, $morder->user_id) !== false ) { 
  51. //updateorderstatus andtransactionids 
  52. $morder->status = "success"; 
  53. $morder->payment_transaction_id = $txn_id; 
  54. if( $recurring ) 
  55. $morder->subscription_transaction_id = $txn_id; 
  56. else 
  57. $morder->subscription_transaction_id = ''; 
  58. $morder->saveOrder(); 
  59.  
  60. //adddiscountcode use 
  61. if(!empty($discount_code) && !empty($use_discount_code)) 
  62. $wpdb->query("INSERT INTO $wpdb->pmpro_discount_codes_uses (code_id, user_id, order_id, timestamp) VALUES('" . $discount_code_id . "', '" . $morder->user_id . "', '" . $morder->id . "', '" . current_time('mysql') . "')"); 
  63.  
  64. //save first and last name fields 
  65. if(!empty($_POST['first_name'])) 
  66. $old_firstname = get_user_meta($morder->user_id, "first_name", true); 
  67. if(!empty($old_firstname)) 
  68. update_user_meta($morder->user_id, "first_name", $_POST['first_name']); 
  69. if(!empty($_POST['last_name'])) 
  70. $old_lastname = get_user_meta($morder->user_id, "last_name", true); 
  71. if(!empty($old_lastname)) 
  72. update_user_meta($morder->user_id, "last_name", $_POST['last_name']); 
  73.  
  74. //hook 
  75. do_action("pmpro_after_checkout", $morder->user_id); 
  76.  
  77. //setup some values for the emails 
  78. if(!empty($morder)) 
  79. $invoice = new MemberOrder($morder->id); 
  80. else 
  81. $invoice = NULL; 
  82.  
  83. inslog("CHANGEMEMBERSHIPLEVEL:ORDER " . var_export($morder, true) . "\n---\n"); 
  84.  
  85. $user = get_userdata($morder->user_id); 
  86. if(empty($user)) 
  87. return false; 
  88.  
  89. $user->membership_level = $morder->membership_level; //make sure they have the right level info 
  90.  
  91. //send email to member 
  92. $pmproemail = new PMProEmail(); 
  93. $pmproemail->sendCheckoutEmail($user, $invoice); 
  94.  
  95. //send email to admin 
  96. $pmproemail = new PMProEmail(); 
  97. $pmproemail->sendCheckoutAdminEmail($user, $invoice); 
  98.  
  99. return true; 
  100. else 
  101. return false;