WC_Payment_Discounts_Apply_Discount

WC_Payment_Discounts_Apply_Discount class.

Defined (1)

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

/includes/class-wc-payment-discounts-apply-discount.php  
  1. class WC_Payment_Discounts_Apply_Discount { 
  2.  
  3. /** 
  4. * Initialize the actions. 
  5. */ 
  6. public function __construct() { 
  7. // Load public-facing JavaScript. 
  8. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  9.  
  10. // Apply the discounts. 
  11. add_action( 'woocommerce_calculate_totals', array( $this, 'add_discount' ), 10 ); 
  12.  
  13. // Display the discount in payment gateways titles. 
  14. add_filter( 'woocommerce_gateway_title', array( $this, 'payment_method_title' ), 10, 2 ); 
  15.  
  16. // Fix salved payment method title and update the cart discount total. 
  17. add_action( 'woocommerce_checkout_order_processed', array( $this, 'update_order_data' ), 10 ); 
  18.  
  19. /** 
  20. * Register and enqueues public-facing JavaScript files. 
  21. */ 
  22. public function enqueue_scripts() { 
  23. if ( is_checkout() ) { 
  24. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  25. wp_enqueue_script( 'woocommerce-payment-discounts', plugins_url( 'assets/js/frontend/update-checkout' . $suffix . '.js', plugin_dir_path( __FILE__ ) ), array( 'wc-checkout' ), WC_Payment_Discounts::VERSION ); 
  26.  
  27. /** 
  28. * Display the discount in payment method title. 
  29. * @param string $title Gateway title. 
  30. * @param string $id Gateway ID. 
  31. * @return string 
  32. */ 
  33. public function payment_method_title( $title, $id ) { 
  34. if ( ! is_checkout() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { 
  35. return $title; 
  36.  
  37. $settings = get_option( 'woocommerce_payment_discounts' ); 
  38.  
  39. if ( isset( $settings[ $id ]['amount'] ) && 0 < $settings[ $id ]['amount'] ) { 
  40. $amount = $settings[ $id ]['amount']; 
  41.  
  42. if ( strstr( $amount, '%' ) ) { 
  43. $value = $amount; 
  44. } else { 
  45. $value = wc_price( $amount ); 
  46.  
  47. $title .= ' <small>(' . sprintf( __( '%s off', 'woocommerce-payment-discounts' ), $value ) . ')</small>'; 
  48.  
  49. return $title; 
  50.  
  51. /** 
  52. * Remove payment coupons. 
  53. * @param WC_Cart $cart Cart object. 
  54. * @param string $skip Payment method ID to skip during remotion. 
  55. * @return bool 
  56. */ 
  57. protected function remove_payment_coupons( $cart, $skip = '' ) { 
  58. $removed = false; 
  59.  
  60. foreach ( $cart->get_applied_coupons() as $code ) { 
  61. if ( 'wcpd_' === substr( $code, 0, 5 ) && $code !== $skip ) { 
  62. $cart->remove_coupon( $code ); 
  63. $removed = true; 
  64.  
  65. return $removed; 
  66.  
  67. /** 
  68. * Add discount. 
  69. * @param WC_Cart $cart Cart object. 
  70. */ 
  71. public function add_discount( $cart ) { 
  72. if ( is_admin() && ! defined( 'DOING_AJAX' ) || is_cart() ) { 
  73. return; 
  74.  
  75. // Gets the settings. 
  76. $settings = get_option( 'woocommerce_payment_discounts' ); 
  77.  
  78. // Always set coupons calculation as enabled. 
  79. add_filter( 'woocommerce_coupons_enabled', '__return_true' ); 
  80.  
  81. if ( isset( $settings[ WC()->session->chosen_payment_method ]['amount'] ) ) { 
  82. // Gets the gateway discount. 
  83. $amount = $settings[ WC()->session->chosen_payment_method ]['amount']; 
  84.  
  85. if ( apply_filters( 'wc_payment_discounts_apply_discount', 0 < $amount, $cart ) ) { 
  86. // Gets the gateway data. 
  87. $methods = WC()->payment_gateways->payment_gateways(); 
  88. $gateway = $methods[ WC()->session->chosen_payment_method ]; 
  89. $coupon = WC_Payment_Discounts_Coupons::get_coupon( $gateway->id, array( 'amount' => $amount ) ); 
  90.  
  91. // Remove other coupons and apply method coupon. 
  92. $this->remove_payment_coupons( $cart, $coupon->get_code() ); 
  93. if ( ! $cart->has_discount( $coupon->get_code() ) ) { 
  94. $cart->add_discount( $coupon->get_code() ); 
  95. } else { 
  96. $this->remove_payment_coupons( $cart ); 
  97.  
  98. /** 
  99. * Remove the discount in the payment method title. 
  100. * @param int $order_id Order ID. 
  101. */ 
  102. public function update_order_data( $order_id ) { 
  103. $payment_method_title = get_post_meta( $order_id, '_payment_method_title', true ); 
  104. $new_payment_method_title = preg_replace( '/<small>.*<\/small>/', '', $payment_method_title ); 
  105.  
  106. // Save the new payment method title. 
  107. update_post_meta( $order_id, '_payment_method_title', $new_payment_method_title );