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