pmpro_ipnChangeMembershipLevel

Change the membership level.

Description

pmpro_ipnChangeMembershipLevel( $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_ipnChangeMembershipLevel' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/services/ipnhandler.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_ipnChangeMembershipLevel($txn_id, $morder); 
  12.  

Defined (1)

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

/services/ipnhandler.php  
  1. function pmpro_ipnChangeMembershipLevel( $txn_id, &$morder ) { 
  2.  
  3. global $wpdb; 
  4.  
  5. //filter for level 
  6. $morder->membership_level = apply_filters( "pmpro_ipnhandler_level", $morder->membership_level, $morder->user_id ); 
  7.  
  8. //set the start date to current_time('timestamp') but allow filters (documented in preheaders/checkout.php) 
  9. $startdate = apply_filters( "pmpro_checkout_start_date", "'" . current_time( 'mysql' ) . "'", $morder->user_id, $morder->membership_level ); 
  10.  
  11. //fix expiration date 
  12. if ( ! empty( $morder->membership_level->expiration_number ) ) { 
  13. $enddate = "'" . date_i18n( "Y-m-d", strtotime( "+ " . $morder->membership_level->expiration_number . " " . $morder->membership_level->expiration_period, current_time( "timestamp" ) ) ) . "'"; 
  14. } else { 
  15. $enddate = "NULL"; 
  16.  
  17. //filter the enddate (documented in preheaders/checkout.php) 
  18. $enddate = apply_filters( "pmpro_checkout_end_date", $enddate, $morder->user_id, $morder->membership_level, $startdate ); 
  19.  
  20. //getdiscountcode 
  21. $morder->getDiscountCode(); 
  22. if ( ! empty( $morder->discount_code ) ) { 
  23. //update membership level 
  24. $morder->getMembershipLevel( true ); 
  25. $discount_code_id = $morder->discount_code->id; 
  26. } else { 
  27. $discount_code_id = ""; 
  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.  
  46. global $pmpro_error; 
  47. if ( ! empty( $pmpro_error ) ) { 
  48. echo $pmpro_error; 
  49. ipnlog( $pmpro_error ); 
  50.  
  51. //change level and continue "checkout" 
  52. if ( pmpro_changeMembershipLevel( $custom_level, $morder->user_id ) !== false ) { 
  53. //updateorderstatus andtransactionids 
  54. $morder->status = "success"; 
  55. $morder->payment_transaction_id = $txn_id; 
  56. if ( ! empty( $_POST['subscr_id'] ) ) { 
  57. $morder->subscription_transaction_id = $_POST['subscr_id']; 
  58. } else { 
  59. $morder->subscription_transaction_id = ""; 
  60. $morder->saveOrder(); 
  61.  
  62. //adddiscountcode use 
  63. if ( ! empty( $discount_code ) && ! empty( $use_discount_code ) ) { 
  64.  
  65. $wpdb->query( 
  66. $wpdb->prepare( 
  67. "INSERT INTO {$wpdb->pmpro_discount_codes_uses}  
  68. ( code_id, user_id, order_id, timestamp )  
  69. VALUES( %d, %d, %s, %s )",  
  70. $discount_code_id),  
  71. $morder->user_id,  
  72. $morder->id,  
  73. current_time( 'mysql' ) 
  74. ); 
  75.  
  76. //save first and last name fields 
  77. if ( ! empty( $_POST['first_name'] ) ) { 
  78. $old_firstname = get_user_meta( $morder->user_id, "first_name", true ); 
  79. if ( empty( $old_firstname ) ) { 
  80. update_user_meta( $morder->user_id, "first_name", $_POST['first_name'] ); 
  81. if ( ! empty( $_POST['last_name'] ) ) { 
  82. $old_lastname = get_user_meta( $morder->user_id, "last_name", true ); 
  83. if ( empty( $old_lastname ) ) { 
  84. update_user_meta( $morder->user_id, "last_name", $_POST['last_name'] ); 
  85.  
  86. //hook 
  87. do_action( "pmpro_after_checkout", $morder->user_id ); 
  88.  
  89. //setup some values for the emails 
  90. if ( ! empty( $morder ) ) { 
  91. $invoice = new MemberOrder( $morder->id ); 
  92. } else { 
  93. $invoice = null; 
  94.  
  95. $user = get_userdata( $morder->user_id ); 
  96. $user->membership_level = $morder->membership_level; //make sure they have the right level info 
  97.  
  98. //send email to member 
  99. $pmproemail = new PMProEmail(); 
  100. $pmproemail->sendCheckoutEmail( $user, $invoice ); 
  101.  
  102. //send email to admin 
  103. $pmproemail = new PMProEmail(); 
  104. $pmproemail->sendCheckoutAdminEmail( $user, $invoice ); 
  105.  
  106. return true; 
  107. } else { 
  108. return false;