1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WooCommerce Account Functions |
4
|
|
|
* |
5
|
|
|
* Functions for account specific things. |
6
|
|
|
* |
7
|
|
|
* @package WooCommerce/Functions |
8
|
|
|
* @version 2.6.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
defined( 'ABSPATH' ) || exit; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Returns the url to the lost password endpoint url. |
15
|
|
|
* |
16
|
|
|
* @param string $default_url Default lost password URL. |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
function wc_lostpassword_url( $default_url = '' ) { |
20
|
|
|
// Avoid loading too early. |
21
|
1 |
|
if ( ! did_action( 'init' ) ) { |
22
|
|
|
return $default_url; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Don't redirect to the woocommerce endpoint on global network admin lost passwords. |
26
|
1 |
|
if ( is_multisite() && isset( $_GET['redirect_to'] ) && false !== strpos( wp_unslash( $_GET['redirect_to'] ), network_admin_url() ) ) { // WPCS: input var ok, sanitization ok, CSRF ok. |
|
|
|
|
27
|
|
|
return $default_url; |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
$wc_account_page_url = wc_get_page_permalink( 'myaccount' ); |
31
|
1 |
|
$wc_account_page_exists = wc_get_page_id( 'myaccount' ) > 0; |
32
|
1 |
|
$lost_password_endpoint = get_option( 'woocommerce_myaccount_lost_password_endpoint' ); |
33
|
|
|
|
34
|
1 |
|
if ( $wc_account_page_exists && ! empty( $lost_password_endpoint ) ) { |
35
|
|
|
return wc_get_endpoint_url( $lost_password_endpoint, '', $wc_account_page_url ); |
36
|
|
|
} else { |
37
|
1 |
|
return $default_url; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
add_filter( 'lostpassword_url', 'wc_lostpassword_url', 10, 1 ); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the link to the edit account details page. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
function wc_customer_edit_account_url() { |
49
|
1 |
|
$edit_account_url = wc_get_endpoint_url( 'edit-account', '', wc_get_page_permalink( 'myaccount' ) ); |
50
|
|
|
|
51
|
1 |
|
return apply_filters( 'woocommerce_customer_edit_account_url', $edit_account_url ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the edit address slug translation. |
56
|
|
|
* |
57
|
|
|
* @param string $id Address ID. |
58
|
|
|
* @param bool $flip Flip the array to make it possible to retrieve the values from both sides. |
59
|
|
|
* |
60
|
|
|
* @return string Address slug i18n. |
61
|
|
|
*/ |
62
|
|
|
function wc_edit_address_i18n( $id, $flip = false ) { |
63
|
1 |
|
$slugs = apply_filters( |
64
|
1 |
|
'woocommerce_edit_address_slugs', |
65
|
|
|
array( |
66
|
1 |
|
'billing' => sanitize_title( _x( 'billing', 'edit-address-slug', 'woocommerce' ) ), |
67
|
1 |
|
'shipping' => sanitize_title( _x( 'shipping', 'edit-address-slug', 'woocommerce' ) ), |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
1 |
|
if ( $flip ) { |
72
|
1 |
|
$slugs = array_flip( $slugs ); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if ( ! isset( $slugs[ $id ] ) ) { |
76
|
|
|
return $id; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return $slugs[ $id ]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get My Account menu items. |
84
|
|
|
* |
85
|
|
|
* @since 2.6.0 |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
function wc_get_account_menu_items() { |
89
|
|
|
$endpoints = array( |
90
|
1 |
|
'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ), |
91
|
1 |
|
'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ), |
92
|
1 |
|
'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ), |
93
|
1 |
|
'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ), |
94
|
1 |
|
'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ), |
95
|
1 |
|
'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ), |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$items = array( |
99
|
1 |
|
'dashboard' => __( 'Dashboard', 'woocommerce' ), |
100
|
1 |
|
'orders' => __( 'Orders', 'woocommerce' ), |
101
|
1 |
|
'downloads' => __( 'Downloads', 'woocommerce' ), |
102
|
1 |
|
'edit-address' => __( 'Addresses', 'woocommerce' ), |
103
|
1 |
|
'payment-methods' => __( 'Payment methods', 'woocommerce' ), |
104
|
1 |
|
'edit-account' => __( 'Account details', 'woocommerce' ), |
105
|
1 |
|
'customer-logout' => __( 'Logout', 'woocommerce' ), |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
// Remove missing endpoints. |
109
|
1 |
|
foreach ( $endpoints as $endpoint_id => $endpoint ) { |
110
|
1 |
|
if ( empty( $endpoint ) ) { |
111
|
|
|
unset( $items[ $endpoint_id ] ); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Check if payment gateways support add new payment methods. |
116
|
1 |
|
if ( isset( $items['payment-methods'] ) ) { |
117
|
1 |
|
$support_payment_methods = false; |
118
|
1 |
|
foreach ( WC()->payment_gateways->get_available_payment_gateways() as $gateway ) { |
119
|
|
|
if ( $gateway->supports( 'add_payment_method' ) || $gateway->supports( 'tokenization' ) ) { |
120
|
|
|
$support_payment_methods = true; |
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
if ( ! $support_payment_methods ) { |
126
|
1 |
|
unset( $items['payment-methods'] ); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return apply_filters( 'woocommerce_account_menu_items', $items, $endpoints ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get account menu item classes. |
135
|
|
|
* |
136
|
|
|
* @since 2.6.0 |
137
|
|
|
* @param string $endpoint Endpoint. |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
function wc_get_account_menu_item_classes( $endpoint ) { |
141
|
|
|
global $wp; |
142
|
|
|
|
143
|
|
|
$classes = array( |
144
|
1 |
|
'woocommerce-MyAccount-navigation-link', |
145
|
1 |
|
'woocommerce-MyAccount-navigation-link--' . $endpoint, |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
// Set current item class. |
149
|
1 |
|
$current = isset( $wp->query_vars[ $endpoint ] ); |
150
|
1 |
|
if ( 'dashboard' === $endpoint && ( isset( $wp->query_vars['page'] ) || empty( $wp->query_vars ) ) ) { |
151
|
|
|
$current = true; // Dashboard is not an endpoint, so needs a custom check. |
152
|
1 |
|
} elseif ( 'orders' === $endpoint && isset( $wp->query_vars['view-order'] ) ) { |
153
|
|
|
$current = true; // When looking at individual order, highlight Orders list item (to signify where in the menu the user currently is). |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
if ( $current ) { |
157
|
|
|
$classes[] = 'is-active'; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
$classes = apply_filters( 'woocommerce_account_menu_item_classes', $classes, $endpoint ); |
161
|
|
|
|
162
|
1 |
|
return implode( ' ', array_map( 'sanitize_html_class', $classes ) ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get account endpoint URL. |
167
|
|
|
* |
168
|
|
|
* @since 2.6.0 |
169
|
|
|
* @param string $endpoint Endpoint. |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
function wc_get_account_endpoint_url( $endpoint ) { |
173
|
1 |
|
if ( 'dashboard' === $endpoint ) { |
174
|
|
|
return wc_get_page_permalink( 'myaccount' ); |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
if ( 'customer-logout' === $endpoint ) { |
178
|
|
|
return wc_logout_url(); |
179
|
|
|
} |
180
|
|
|
|
181
|
1 |
|
return wc_get_endpoint_url( $endpoint, '', wc_get_page_permalink( 'myaccount' ) ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get My Account > Orders columns. |
186
|
|
|
* |
187
|
|
|
* @since 2.6.0 |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
function wc_get_account_orders_columns() { |
191
|
1 |
|
$columns = apply_filters( |
192
|
1 |
|
'woocommerce_account_orders_columns', |
193
|
|
|
array( |
194
|
1 |
|
'order-number' => __( 'Order', 'woocommerce' ), |
195
|
1 |
|
'order-date' => __( 'Date', 'woocommerce' ), |
196
|
1 |
|
'order-status' => __( 'Status', 'woocommerce' ), |
197
|
1 |
|
'order-total' => __( 'Total', 'woocommerce' ), |
198
|
1 |
|
'order-actions' => __( 'Actions', 'woocommerce' ), |
199
|
|
|
) |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
// Deprecated filter since 2.6.0. |
203
|
1 |
|
return apply_filters( 'woocommerce_my_account_my_orders_columns', $columns ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get My Account > Downloads columns. |
208
|
|
|
* |
209
|
|
|
* @since 2.6.0 |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
|
|
function wc_get_account_downloads_columns() { |
213
|
1 |
|
$columns = apply_filters( |
214
|
1 |
|
'woocommerce_account_downloads_columns', |
215
|
|
|
array( |
216
|
1 |
|
'download-product' => __( 'Product', 'woocommerce' ), |
217
|
1 |
|
'download-remaining' => __( 'Downloads remaining', 'woocommerce' ), |
218
|
1 |
|
'download-expires' => __( 'Expires', 'woocommerce' ), |
219
|
1 |
|
'download-file' => __( 'Download', 'woocommerce' ), |
220
|
1 |
|
'download-actions' => ' ', |
221
|
|
|
) |
222
|
|
|
); |
223
|
|
|
|
224
|
1 |
|
if ( ! has_filter( 'woocommerce_account_download_actions' ) ) { |
225
|
1 |
|
unset( $columns['download-actions'] ); |
226
|
|
|
} |
227
|
|
|
|
228
|
1 |
|
return $columns; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get My Account > Payment methods columns. |
233
|
|
|
* |
234
|
|
|
* @since 2.6.0 |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
function wc_get_account_payment_methods_columns() { |
238
|
1 |
|
return apply_filters( |
239
|
1 |
|
'woocommerce_account_payment_methods_columns', |
240
|
|
|
array( |
241
|
1 |
|
'method' => __( 'Method', 'woocommerce' ), |
242
|
1 |
|
'expires' => __( 'Expires', 'woocommerce' ), |
243
|
1 |
|
'actions' => ' ', |
244
|
|
|
) |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get My Account > Payment methods types |
250
|
|
|
* |
251
|
|
|
* @since 2.6.0 |
252
|
|
|
* @return array |
253
|
|
|
*/ |
254
|
|
|
function wc_get_account_payment_methods_types() { |
255
|
1 |
|
return apply_filters( |
256
|
1 |
|
'woocommerce_payment_methods_types', |
257
|
|
|
array( |
258
|
1 |
|
'cc' => __( 'Credit card', 'woocommerce' ), |
259
|
1 |
|
'echeck' => __( 'eCheck', 'woocommerce' ), |
260
|
|
|
) |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Get account orders actions. |
266
|
|
|
* |
267
|
|
|
* @since 3.2.0 |
268
|
|
|
* @param int|WC_Order $order Order instance or ID. |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
|
|
function wc_get_account_orders_actions( $order ) { |
272
|
1 |
|
if ( ! is_object( $order ) ) { |
273
|
1 |
|
$order_id = absint( $order ); |
274
|
1 |
|
$order = wc_get_order( $order_id ); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$actions = array( |
278
|
|
|
'pay' => array( |
279
|
1 |
|
'url' => $order->get_checkout_payment_url(), |
280
|
1 |
|
'name' => __( 'Pay', 'woocommerce' ), |
281
|
|
|
), |
282
|
|
|
'view' => array( |
283
|
1 |
|
'url' => $order->get_view_order_url(), |
284
|
1 |
|
'name' => __( 'View', 'woocommerce' ), |
285
|
|
|
), |
286
|
|
|
'cancel' => array( |
287
|
1 |
|
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ), |
288
|
1 |
|
'name' => __( 'Cancel', 'woocommerce' ), |
289
|
|
|
), |
290
|
|
|
); |
291
|
|
|
|
292
|
1 |
|
if ( ! $order->needs_payment() ) { |
293
|
|
|
unset( $actions['pay'] ); |
294
|
|
|
} |
295
|
|
|
|
296
|
1 |
|
if ( ! in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ), true ) ) { |
297
|
|
|
unset( $actions['cancel'] ); |
298
|
|
|
} |
299
|
|
|
|
300
|
1 |
|
return apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order ); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Get account formatted address. |
305
|
|
|
* |
306
|
|
|
* @since 3.2.0 |
307
|
|
|
* @param string $address_type Address type. |
308
|
|
|
* Accepts: 'billing' or 'shipping'. |
309
|
|
|
* Default to 'billing'. |
310
|
|
|
* @param int $customer_id Customer ID. |
311
|
|
|
* Default to 0. |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
|
|
function wc_get_account_formatted_address( $address_type = 'billing', $customer_id = 0 ) { |
315
|
1 |
|
$getter = "get_{$address_type}"; |
316
|
1 |
|
$address = array(); |
317
|
|
|
|
318
|
1 |
|
if ( 0 === $customer_id ) { |
319
|
|
|
$customer_id = get_current_user_id(); |
320
|
|
|
} |
321
|
|
|
|
322
|
1 |
|
$customer = new WC_Customer( $customer_id ); |
323
|
|
|
|
324
|
1 |
|
if ( is_callable( array( $customer, $getter ) ) ) { |
325
|
1 |
|
$address = $customer->$getter(); |
326
|
1 |
|
unset( $address['email'], $address['tel'] ); |
327
|
|
|
} |
328
|
|
|
|
329
|
1 |
|
return WC()->countries->get_formatted_address( apply_filters( 'woocommerce_my_account_my_address_formatted_address', $address, $customer->get_id(), $address_type ) ); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Returns an array of a user's saved payments list for output on the account tab. |
334
|
|
|
* |
335
|
|
|
* @since 2.6 |
336
|
|
|
* @param array $list List of payment methods passed from wc_get_customer_saved_methods_list(). |
337
|
|
|
* @param int $customer_id The customer to fetch payment methods for. |
338
|
|
|
* @return array Filtered list of customers payment methods. |
339
|
|
|
*/ |
340
|
|
|
function wc_get_account_saved_payment_methods_list( $list, $customer_id ) { |
341
|
1 |
|
$payment_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id ); |
342
|
1 |
|
foreach ( $payment_tokens as $payment_token ) { |
343
|
1 |
|
$delete_url = wc_get_endpoint_url( 'delete-payment-method', $payment_token->get_id() ); |
344
|
1 |
|
$delete_url = wp_nonce_url( $delete_url, 'delete-payment-method-' . $payment_token->get_id() ); |
345
|
1 |
|
$set_default_url = wc_get_endpoint_url( 'set-default-payment-method', $payment_token->get_id() ); |
346
|
1 |
|
$set_default_url = wp_nonce_url( $set_default_url, 'set-default-payment-method-' . $payment_token->get_id() ); |
347
|
|
|
|
348
|
1 |
|
$type = strtolower( $payment_token->get_type() ); |
349
|
1 |
|
$list[ $type ][] = array( |
350
|
|
|
'method' => array( |
351
|
1 |
|
'gateway' => $payment_token->get_gateway_id(), |
352
|
|
|
), |
353
|
1 |
|
'expires' => esc_html__( 'N/A', 'woocommerce' ), |
354
|
1 |
|
'is_default' => $payment_token->is_default(), |
355
|
|
|
'actions' => array( |
356
|
|
|
'delete' => array( |
357
|
1 |
|
'url' => $delete_url, |
358
|
1 |
|
'name' => esc_html__( 'Delete', 'woocommerce' ), |
359
|
|
|
), |
360
|
|
|
), |
361
|
|
|
); |
362
|
1 |
|
$key = key( array_slice( $list[ $type ], -1, 1, true ) ); |
363
|
|
|
|
364
|
1 |
|
if ( ! $payment_token->is_default() ) { |
365
|
|
|
$list[ $type ][ $key ]['actions']['default'] = array( |
366
|
|
|
'url' => $set_default_url, |
367
|
|
|
'name' => esc_html__( 'Make default', 'woocommerce' ), |
368
|
|
|
); |
369
|
|
|
} |
370
|
|
|
|
371
|
1 |
|
$list[ $type ][ $key ] = apply_filters( 'woocommerce_payment_methods_list_item', $list[ $type ][ $key ], $payment_token ); |
372
|
|
|
} |
373
|
1 |
|
return $list; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
add_filter( 'woocommerce_saved_payment_methods_list', 'wc_get_account_saved_payment_methods_list', 10, 2 ); |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Controls the output for credit cards on the my account page. |
380
|
|
|
* |
381
|
|
|
* @since 2.6 |
382
|
|
|
* @param array $item Individual list item from woocommerce_saved_payment_methods_list. |
383
|
|
|
* @param WC_Payment_Token $payment_token The payment token associated with this method entry. |
384
|
|
|
* @return array Filtered item. |
385
|
|
|
*/ |
386
|
|
|
function wc_get_account_saved_payment_methods_list_item_cc( $item, $payment_token ) { |
387
|
2 |
|
if ( 'cc' !== strtolower( $payment_token->get_type() ) ) { |
388
|
|
|
return $item; |
389
|
|
|
} |
390
|
|
|
|
391
|
2 |
|
$card_type = $payment_token->get_card_type(); |
392
|
2 |
|
$item['method']['last4'] = $payment_token->get_last4(); |
393
|
2 |
|
$item['method']['brand'] = ( ! empty( $card_type ) ? ucfirst( $card_type ) : esc_html__( 'Credit card', 'woocommerce' ) ); |
394
|
2 |
|
$item['expires'] = $payment_token->get_expiry_month() . '/' . substr( $payment_token->get_expiry_year(), -2 ); |
395
|
|
|
|
396
|
2 |
|
return $item; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
add_filter( 'woocommerce_payment_methods_list_item', 'wc_get_account_saved_payment_methods_list_item_cc', 10, 2 ); |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Controls the output for eChecks on the my account page. |
403
|
|
|
* |
404
|
|
|
* @since 2.6 |
405
|
|
|
* @param array $item Individual list item from woocommerce_saved_payment_methods_list. |
406
|
|
|
* @param WC_Payment_Token $payment_token The payment token associated with this method entry. |
407
|
|
|
* @return array Filtered item. |
408
|
|
|
*/ |
409
|
|
|
function wc_get_account_saved_payment_methods_list_item_echeck( $item, $payment_token ) { |
410
|
2 |
|
if ( 'echeck' !== strtolower( $payment_token->get_type() ) ) { |
411
|
1 |
|
return $item; |
412
|
|
|
} |
413
|
|
|
|
414
|
1 |
|
$item['method']['last4'] = $payment_token->get_last4(); |
415
|
1 |
|
$item['method']['brand'] = esc_html__( 'eCheck', 'woocommerce' ); |
416
|
|
|
|
417
|
1 |
|
return $item; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
add_filter( 'woocommerce_payment_methods_list_item', 'wc_get_account_saved_payment_methods_list_item_echeck', 10, 2 ); |
421
|
|
|
|