class-wc-gateway-cheque.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Class WC_Gateway_Cheque file.
  4. *
  5. * @package WooCommerce\Gateways
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit; // Exit if accessed directly.
  9. }
  10. /**
  11. * Cheque Payment Gateway.
  12. *
  13. * Provides a Cheque Payment Gateway, mainly for testing purposes.
  14. *
  15. * @class WC_Gateway_Cheque
  16. * @extends WC_Payment_Gateway
  17. * @version 2.1.0
  18. * @package WooCommerce/Classes/Payment
  19. */
  20. class WC_Gateway_Cheque extends WC_Payment_Gateway {
  21. /**
  22. * Constructor for the gateway.
  23. */
  24. public function __construct() {
  25. $this->id = 'cheque';
  26. $this->icon = apply_filters( 'woocommerce_cheque_icon', '' );
  27. $this->has_fields = false;
  28. $this->method_title = _x( 'Check payments', 'Check payment method', 'woocommerce' );
  29. $this->method_description = __( 'Take payments in person via checks. This offline gateway can also be useful to test purchases.', 'woocommerce' );
  30. // Load the settings.
  31. $this->init_form_fields();
  32. $this->init_settings();
  33. // Define user set variables.
  34. $this->title = $this->get_option( 'title' );
  35. $this->description = $this->get_option( 'description' );
  36. $this->instructions = $this->get_option( 'instructions' );
  37. // Actions.
  38. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
  39. add_action( 'woocommerce_thankyou_cheque', array( $this, 'thankyou_page' ) );
  40. // Customer Emails.
  41. add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
  42. }
  43. /**
  44. * Initialise Gateway Settings Form Fields.
  45. */
  46. public function init_form_fields() {
  47. $this->form_fields = array(
  48. 'enabled' => array(
  49. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  50. 'type' => 'checkbox',
  51. 'label' => __( 'Enable check payments', 'woocommerce' ),
  52. 'default' => 'no',
  53. ),
  54. 'title' => array(
  55. 'title' => __( 'Title', 'woocommerce' ),
  56. 'type' => 'text',
  57. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  58. 'default' => _x( 'Check payments', 'Check payment method', 'woocommerce' ),
  59. 'desc_tip' => true,
  60. ),
  61. 'description' => array(
  62. 'title' => __( 'Description', 'woocommerce' ),
  63. 'type' => 'textarea',
  64. 'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
  65. 'default' => __( 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', 'woocommerce' ),
  66. 'desc_tip' => true,
  67. ),
  68. 'instructions' => array(
  69. 'title' => __( 'Instructions', 'woocommerce' ),
  70. 'type' => 'textarea',
  71. 'description' => __( 'Instructions that will be added to the thank you page and emails.', 'woocommerce' ),
  72. 'default' => '',
  73. 'desc_tip' => true,
  74. ),
  75. );
  76. }
  77. /**
  78. * Output for the order received page.
  79. */
  80. public function thankyou_page() {
  81. if ( $this->instructions ) {
  82. echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) );
  83. }
  84. }
  85. /**
  86. * Add content to the WC emails.
  87. *
  88. * @access public
  89. * @param WC_Order $order Order object.
  90. * @param bool $sent_to_admin Sent to admin.
  91. * @param bool $plain_text Email format: plain text or HTML.
  92. */
  93. public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
  94. if ( $this->instructions && ! $sent_to_admin && 'cheque' === $order->get_payment_method() && $order->has_status( 'on-hold' ) ) {
  95. echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) . PHP_EOL );
  96. }
  97. }
  98. /**
  99. * Process the payment and return the result.
  100. *
  101. * @param int $order_id Order ID.
  102. * @return array
  103. */
  104. public function process_payment( $order_id ) {
  105. $order = wc_get_order( $order_id );
  106. if ( $order->get_total() > 0 ) {
  107. // Mark as on-hold (we're awaiting the cheque).
  108. $order->update_status( 'on-hold', _x( 'Awaiting check payment', 'Check payment method', 'woocommerce' ) );
  109. } else {
  110. $order->payment_complete();
  111. }
  112. // Reduce stock levels.
  113. wc_reduce_stock_levels( $order_id );
  114. // Remove cart.
  115. WC()->cart->empty_cart();
  116. // Return thankyou redirect.
  117. return array(
  118. 'result' => 'success',
  119. 'redirect' => $this->get_return_url( $order ),
  120. );
  121. }
  122. }