pmpro_getLevelAtCheckout

Get level at checkout and place into $pmpro_level global.

Description

pmpro_getLevelAtCheckout( (constant) $level_id = NULL, (constant) $discount_code = NULL ); 

Parameters (2)

0. $level_id — Optional. (constant) => NULL
The level id.
1. $discount_code — Optional. (constant) => NULL
The discount code.

Usage

  1. if ( !function_exists( 'pmpro_getLevelAtCheckout' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/functions.php'; 
  3.  
  4. // The level id. 
  5. $level_id = NULL; 
  6.  
  7. // The discount code. 
  8. $discount_code = NULL; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pmpro_getLevelAtCheckout($level_id, $discount_code); 
  12.  

Defined (1)

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

/includes/functions.php  
  1. function pmpro_getLevelAtCheckout($level_id = NULL, $discount_code = NULL) { 
  2. global $pmpro_level, $wpdb, $post; 
  3.  
  4. //reset pmpro_level 
  5. $pmpro_level = NULL; 
  6.  
  7. //default to level passed in via URL 
  8. if(empty($level_id) && !empty($_REQUEST['level'])) { 
  9. $level_id = intval($_REQUEST['level']); 
  10.  
  11. //default todiscountcode passed in 
  12. if(empty($discount_code) && !empty($_REQUEST['discount_code'])) { 
  13. $discount_code = preg_replace( "/[^A-Za-z0-9\-]/", "", $_REQUEST['discount_code'] ); 
  14.  
  15. //what level are they purchasing? (discount code passed) 
  16. if (!empty($level_id) && !empty($discount_code)) {  
  17. $discount_code_id = $wpdb->get_var( "SELECT id FROM $wpdb->pmpro_discount_codes WHERE code = '" . $discount_code . "' LIMIT 1" ); 
  18.  
  19. //check code 
  20. $code_check = pmpro_checkDiscountCode( $discount_code, $level_id, true ); 
  21. if ( $code_check[0] != false ) {  
  22. $sqlQuery = "SELECT l.id, cl.*, l.name, l.description, l.allow_signups FROM $wpdb->pmpro_discount_codes_levels cl LEFT JOIN $wpdb->pmpro_membership_levels l ON cl.level_id = l.id LEFT JOIN $wpdb->pmpro_discount_codes dc ON dc.id = cl.code_id WHERE dc.code = '" . $discount_code . "' AND cl.level_id = '" . $level_id . "' LIMIT 1"; 
  23. $pmpro_level = $wpdb->get_row( $sqlQuery ); 
  24.  
  25. //if thediscountcode doesn't adjust the level, let's just get the straight level 
  26. if ( empty( $pmpro_level ) ) { 
  27. $pmpro_level = $wpdb->get_row( "SELECT * FROM $wpdb->pmpro_membership_levels WHERE id = '" . $level_id . "' LIMIT 1" ); 
  28.  
  29. //filter adjustments to the level 
  30. $pmpro_level->code_id = $discount_code_id; 
  31. $pmpro_level = apply_filters( "pmpro_discount_code_level", $pmpro_level, $discount_code_id ); 
  32. } else { 
  33. //error withdiscountcode, we want to halt checkout 
  34. pmpro_setMessage($code_check[1], 'pmpro_error'); 
  35.  
  36. //what level are they purchasing? (nodiscountcode) 
  37. if ( empty( $pmpro_level ) && ! empty( $level_id ) ) { 
  38. $pmpro_level = $wpdb->get_row( "SELECT * FROM $wpdb->pmpro_membership_levels WHERE id = '" . esc_sql( $level_id ) . "' AND allow_signups = 1 LIMIT 1" ); 
  39. } elseif ( empty( $pmpro_level ) && !empty( $post ) ) { 
  40. //check if a level is defined in custom fields 
  41. $default_level = get_post_meta( $post->ID, "pmpro_default_level", true ); 
  42. if ( ! empty( $default_level ) ) { 
  43. $pmpro_level = $wpdb->get_row( "SELECT * FROM $wpdb->pmpro_membership_levels WHERE id = '" . esc_sql( $default_level ) . "' AND allow_signups = 1 LIMIT 1" ); 
  44.  
  45. //filter the level (for upgrades, etc) 
  46. $pmpro_level = apply_filters( "pmpro_checkout_level", $pmpro_level ); 
  47.  
  48. return $pmpro_level;