wc-account-functions.php 13 KB

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