WC_Payment_Discounts_Coupons

WC_Payment_Discounts_Coupons class.

Defined (1)

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

/includes/class-wc-payment-discounts-coupons.php  
  1. class WC_Payment_Discounts_Coupons { 
  2.  
  3. /** 
  4. * Generate coupon code. 
  5. * @param string $payment_method Payment method ID. 
  6. * @return string 
  7. */ 
  8. protected static function generate_code( $payment_method ) { 
  9. $code = 'wcpd_' . $payment_method . '_' . wp_generate_password( 20, false ); 
  10.  
  11. // Remove "_" and empty spaces. 
  12. $payment_method = str_replace( ' ', '', $payment_method ); 
  13.  
  14. return $code; 
  15.  
  16. /** 
  17. * Query coupon ID. 
  18. * @param string $payment_method Payment method ID. 
  19. * @return int|null 
  20. */ 
  21. protected static function get_coupon_id( $payment_method ) { 
  22. global $wpdb; 
  23.  
  24. return $wpdb->get_var( $wpdb->prepare( " 
  25. SELECT post_id 
  26. FROM {$wpdb->postmeta} 
  27. WHERE meta_key = '_wcpd_payment_method' 
  28. AND meta_value = '%s' 
  29. ", $payment_method ) ); 
  30.  
  31. /** 
  32. * Get coupon by payment method ID. 
  33. * @param string $payment_method Payment method ID. 
  34. * @param array $data Coupon data. 
  35. * @return WC_Coupon 
  36. */ 
  37. public static function get_coupon( $payment_method, $data = array() ) { 
  38. $id = self::get_coupon_id( $payment_method ); 
  39.  
  40. if ( is_null( $id ) ) { 
  41. return self::create_coupon( $payment_method, $data ); 
  42.  
  43. return new WC_Coupon( intval( $id ) ); 
  44.  
  45. /** 
  46. * Create coupon for a payment method. 
  47. * @param string $payment_method Payment method ID. 
  48. * @param array $data Coupon data. 
  49. * @return WC_Coupon 
  50. */ 
  51. public static function create_coupon( $payment_method, $data ) { 
  52. $coupon = new WC_Coupon(); 
  53.  
  54. $code = wc_format_coupon_code( self::generate_code( $payment_method ) ); 
  55. $type = false !== strstr( $data['amount'], '%' ) ? 'percent' : 'fixed_cart'; 
  56. $amount = (float) wc_format_decimal( trim( $data['amount'], '%' ) ); 
  57.  
  58. $coupon->set_code( $code ); 
  59. $coupon->set_description( esc_html( sprintf( __( 'Discount coupon for payments with %s.', 'woocommerce-payment-discounts' ), $payment_method ) ) ); 
  60. $coupon->set_amount( $amount ); 
  61. $coupon->set_discount_type( $type ); 
  62. $coupon->add_meta_data( '_wcpd_payment_method', $payment_method, true ); 
  63. $coupon->save(); 
  64.  
  65. return $coupon; 
  66.  
  67. /** 
  68. * Update payment method coupon. 
  69. * @param string $payment_method Payment method ID. 
  70. * @param array $data Coupon data. 
  71. * @return bool 
  72. */ 
  73. public static function update_coupon( $payment_method, $data ) { 
  74. $id = (int) self::get_coupon_id( $payment_method ); 
  75. $amount = (float) wc_format_decimal( trim( $data['amount'], '%' ) ); 
  76.  
  77. // Ignore if don't exists and amount is 0. 
  78. if ( (float) 0 === $amount && 0 === $id ) { 
  79. return false; 
  80.  
  81. // Delete existing coupon if amount is 0. 
  82. if ( (float) 0 === $amount && 0 !== $id ) { 
  83. $coupon = new WC_Coupon( (int) $id ); 
  84. $coupon->delete( true ); 
  85. return true; 
  86.  
  87. // Create a new coupon. 
  88. if ( 0 === $id ) { 
  89. self::create_coupon( $payment_method, $data ); 
  90. return true; 
  91.  
  92. // Update existing coupon. 
  93. $coupon = new WC_Coupon( (int) $id ); 
  94. $type = false !== strstr( $data['amount'], '%' ) ? 'percent' : 'fixed_cart'; 
  95.  
  96. $coupon->set_amount( $amount ); 
  97. $coupon->set_discount_type( $type ); 
  98. $coupon->save(); 
  99.  
  100. return true;