pmpro_checkDiscountCode

The Paid Memberships Pro pmpro checkDiscountCode function.

Description

pmpro_checkDiscountCode( $code, (constant) $level_id = NULL, (constant) $return_errors = false ); 

Parameters (3)

0. $code
The code.
1. $level_id — Optional. (constant) => NULL
The level id.
2. $return_errors — Optional. (constant) => false
The return errors.

Usage

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

Defined (1)

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

/includes/functions.php  
  1. function pmpro_checkDiscountCode($code, $level_id = NULL, $return_errors = false) 
  2. global $wpdb; 
  3.  
  4. $error = false; 
  5. $dbcode = false; 
  6.  
  7. //no code, no code 
  8. if(empty($code)) 
  9. $error = __("No code was given to check.", 'paid-memberships-pro' ); 
  10.  
  11. //get code from db 
  12. if(!$error) 
  13. $dbcode = $wpdb->get_row("SELECT *, UNIX_TIMESTAMP(starts) as starts, UNIX_TIMESTAMP(expires) as expires FROM $wpdb->pmpro_discount_codes WHERE code ='" . esc_sql($code) . "' LIMIT 1"); 
  14.  
  15. //did we find it? 
  16. if(empty($dbcode->id)) 
  17. $error = __("Thediscountcode could not be found.", 'paid-memberships-pro' ); 
  18.  
  19. //check if the code has started 
  20. if(!$error) 
  21. //fix the date timestamps 
  22. $dbcode->starts = strtotime(date_i18n("m/d/Y", $dbcode->starts)); 
  23. $dbcode->expires = strtotime(date_i18n("m/d/Y", $dbcode->expires)); 
  24.  
  25. //today 
  26. $today = strtotime(date_i18n("m/d/Y 00:00:00", current_time("timestamp"))); 
  27.  
  28. //has this code started yet? 
  29. if(!empty($dbcode->starts) && $dbcode->starts > $today) 
  30. $error = sprintf(__("Thisdiscountcode goes into effect on %s.", 'paid-memberships-pro' ), date_i18n(get_option('date_format'), $dbcode->starts)); 
  31.  
  32. //check if the code is expired 
  33. if(!$error) 
  34. if(!empty($dbcode->expires) && $dbcode->expires < $today) 
  35. $error = sprintf(__("Thisdiscountcode expired on %s.", 'paid-memberships-pro' ), date_i18n(get_option('date_format'), $dbcode->expires)); 
  36.  
  37. //have we run out of uses? 
  38. if(!$error) 
  39. if($dbcode->uses > 0) 
  40. $used = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->pmpro_discount_codes_uses WHERE code_id = '" . $dbcode->id . "'"); 
  41. if($used >= $dbcode->uses) 
  42. $error = __("Thisdiscountcode is no longer valid.", 'paid-memberships-pro' ); 
  43.  
  44. //if a level was passed check if this code applies 
  45. if(!$error) 
  46. $pmpro_check_discount_code_levels = apply_filters("pmpro_check_discount_code_levels", true, $dbcode->id); 
  47. if(!empty($level_id) && $pmpro_check_discount_code_levels) 
  48. // clean up level id for security before the database call 
  49. if(is_array($level_id)) { 
  50. $levelnums = array_map('intval', $level_id); 
  51. $level_id = implode(', ', $levelnums); 
  52. } else { 
  53. $level_id = intval($level_id); 
  54. $code_level = $wpdb->get_row("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 WHERE cl.code_id = '" . $dbcode->id . "' AND cl.level_id IN (" . $level_id . ") LIMIT 1"); 
  55.  
  56. if(empty($code_level)) 
  57. $error = __("Thisdiscountcode does not apply to this membership level.", 'paid-memberships-pro' ); 
  58.  
  59. /** 
  60. * Filter the results of thediscountcode check.  
  61. * @since 1.7.13.1 
  62. * @param bool $okay true if code check is okay or false if there was an error 
  63. * @param object $dbcode Object containing code data from the database row 
  64. * @param int $level_id ID of the level the user is checking out for. 
  65. * @param string $code Discount code string. 
  66. *  
  67. * @return mixed $okay true if okay, false orerrormessage string if not okay 
  68. */ 
  69. $okay = !$error; 
  70. $pmpro_check_discount_code = apply_filters("pmpro_check_discount_code", $okay, $dbcode, $level_id, $code); 
  71. if(is_string($pmpro_check_discount_code)) 
  72. $error = $pmpro_check_discount_code; //string returned, this is an error 
  73. elseif(!$pmpro_check_discount_code && !$error) 
  74. $error = true; //noerrorbefore, but filter returned error 
  75. elseif($pmpro_check_discount_code) 
  76. $error = false; //filter is true, soerrorfalse 
  77.  
  78. //return 
  79. if($error) 
  80. //there was an error 
  81. if(!empty($return_errors)) 
  82. return array(false, $error); 
  83. else 
  84. return false; 
  85. else 
  86. //guess we're all good 
  87. if(!empty($return_errors)) 
  88. return array(true, __("Thisdiscountcode is okay.", 'paid-memberships-pro' )); 
  89. else 
  90. return true;