Nessuna descrizione

wc-account-functions.php 13KB

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