/wp-content/plugins/woocommerce/includes/admin/list-tables/class-wc-admin-list-table-orders.php

https://bitbucket.org/karimlm/ifb.tn · PHP · 838 lines · 541 code · 121 blank · 176 comment · 64 complexity · aef94a9f874cb1142e364c9f45d15f20 MD5 · raw file

  1. <?php
  2. /**
  3. * List tables: orders.
  4. *
  5. * @author WooCommerce
  6. * @category Admin
  7. * @package WooCommerce/Admin
  8. * @version 3.3.0
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11. exit;
  12. }
  13. if ( class_exists( 'WC_Admin_List_Table_Orders', false ) ) {
  14. return;
  15. }
  16. if ( ! class_exists( 'WC_Admin_List_Table', false ) ) {
  17. include_once 'abstract-class-wc-admin-list-table.php';
  18. }
  19. /**
  20. * WC_Admin_List_Table_Orders Class.
  21. */
  22. class WC_Admin_List_Table_Orders extends WC_Admin_List_Table {
  23. /**
  24. * Post type.
  25. *
  26. * @var string
  27. */
  28. protected $list_table_type = 'shop_order';
  29. /**
  30. * Constructor.
  31. */
  32. public function __construct() {
  33. parent::__construct();
  34. add_action( 'admin_notices', array( $this, 'bulk_admin_notices' ) );
  35. add_action( 'admin_footer', array( $this, 'order_preview_template' ) );
  36. add_filter( 'get_search_query', array( $this, 'search_label' ) );
  37. add_filter( 'query_vars', array( $this, 'add_custom_query_var' ) );
  38. add_action( 'parse_query', array( $this, 'search_custom_fields' ) );
  39. }
  40. /**
  41. * Render blank state.
  42. */
  43. protected function render_blank_state() {
  44. echo '<div class="woocommerce-BlankState">';
  45. echo '<h2 class="woocommerce-BlankState-message">' . esc_html__( 'When you receive a new order, it will appear here.', 'woocommerce' ) . '</h2>';
  46. echo '<a class="woocommerce-BlankState-cta button-primary button" target="_blank" href="https://docs.woocommerce.com/document/managing-orders/?utm_source=blankslate&utm_medium=product&utm_content=ordersdoc&utm_campaign=woocommerceplugin">' . esc_html__( 'Learn more about orders', 'woocommerce' ) . '</a>';
  47. echo '</div>';
  48. }
  49. /**
  50. * Define primary column.
  51. *
  52. * @return string
  53. */
  54. protected function get_primary_column() {
  55. return 'order_number';
  56. }
  57. /**
  58. * Get row actions to show in the list table.
  59. *
  60. * @param array $actions Array of actions.
  61. * @param WP_Post $post Current post object.
  62. * @return array
  63. */
  64. protected function get_row_actions( $actions, $post ) {
  65. return array();
  66. }
  67. /**
  68. * Define hidden columns.
  69. *
  70. * @return array
  71. */
  72. protected function define_hidden_columns() {
  73. return array(
  74. 'shipping_address',
  75. 'billing_address',
  76. 'wc_actions',
  77. );
  78. }
  79. /**
  80. * Define which columns are sortable.
  81. *
  82. * @param array $columns Existing columns.
  83. * @return array
  84. */
  85. public function define_sortable_columns( $columns ) {
  86. $custom = array(
  87. 'order_number' => 'ID',
  88. 'order_total' => 'order_total',
  89. 'order_date' => 'date',
  90. );
  91. unset( $columns['comments'] );
  92. return wp_parse_args( $custom, $columns );
  93. }
  94. /**
  95. * Define which columns to show on this screen.
  96. *
  97. * @param array $columns Existing columns.
  98. * @return array
  99. */
  100. public function define_columns( $columns ) {
  101. $show_columns = array();
  102. $show_columns['cb'] = $columns['cb'];
  103. $show_columns['order_number'] = __( 'Order', 'woocommerce' );
  104. $show_columns['order_date'] = __( 'Date', 'woocommerce' );
  105. $show_columns['order_status'] = __( 'Status', 'woocommerce' );
  106. $show_columns['billing_address'] = __( 'Billing', 'woocommerce' );
  107. $show_columns['shipping_address'] = __( 'Ship to', 'woocommerce' );
  108. $show_columns['order_total'] = __( 'Total', 'woocommerce' );
  109. $show_columns['wc_actions'] = __( 'Actions', 'woocommerce' );
  110. wp_enqueue_script( 'wc-orders' );
  111. return $show_columns;
  112. }
  113. /**
  114. * Define bulk actions.
  115. *
  116. * @param array $actions Existing actions.
  117. * @return array
  118. */
  119. public function define_bulk_actions( $actions ) {
  120. if ( isset( $actions['edit'] ) ) {
  121. unset( $actions['edit'] );
  122. }
  123. $actions['mark_processing'] = __( 'Change status to processing', 'woocommerce' );
  124. $actions['mark_on-hold'] = __( 'Change status to on-hold', 'woocommerce' );
  125. $actions['mark_completed'] = __( 'Change status to completed', 'woocommerce' );
  126. return $actions;
  127. }
  128. /**
  129. * Pre-fetch any data for the row each column has access to it. the_order global is there for bw compat.
  130. *
  131. * @param int $post_id Post ID being shown.
  132. */
  133. protected function prepare_row_data( $post_id ) {
  134. global $the_order;
  135. if ( empty( $this->object ) || $this->object->get_id() !== $post_id ) {
  136. $this->object = wc_get_order( $post_id );
  137. $the_order = $this->object;
  138. }
  139. }
  140. /**
  141. * Render columm: order_number.
  142. */
  143. protected function render_order_number_column() {
  144. $buyer = '';
  145. if ( $this->object->get_billing_first_name() || $this->object->get_billing_last_name() ) {
  146. /* translators: 1: first name 2: last name */
  147. $buyer = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $this->object->get_billing_first_name(), $this->object->get_billing_last_name() ) );
  148. } elseif ( $this->object->get_billing_company() ) {
  149. $buyer = trim( $this->object->get_billing_company() );
  150. } elseif ( $this->object->get_customer_id() ) {
  151. $user = get_user_by( 'id', $this->object->get_customer_id() );
  152. $buyer = ucwords( $user->display_name );
  153. }
  154. if ( $this->object->get_status() === 'trash' ) {
  155. echo '<strong>#' . esc_attr( $this->object->get_order_number() ) . ' ' . esc_html( $buyer ) . '</strong>';
  156. } else {
  157. echo '<a href="#" class="order-preview" data-order-id="' . absint( $this->object->get_id() ) . '" title="' . esc_attr( __( 'Preview', 'woocommerce' ) ) . '">' . esc_html( __( 'Preview', 'woocommerce' ) ) . '</a>';
  158. echo '<a href="' . esc_url( admin_url( 'post.php?post=' . absint( $this->object->get_id() ) ) . '&action=edit' ) . '" class="order-view"><strong>#' . esc_attr( $this->object->get_order_number() ) . ' ' . esc_html( $buyer ) . '</strong></a>';
  159. }
  160. }
  161. /**
  162. * Render columm: order_status.
  163. */
  164. protected function render_order_status_column() {
  165. $tooltip = '';
  166. $comment_count = get_comment_count( $this->object->get_id() );
  167. $approved_comments_count = absint( $comment_count['approved'] );
  168. if ( $approved_comments_count ) {
  169. $latest_notes = wc_get_order_notes(
  170. array(
  171. 'order_id' => $this->object->get_id(),
  172. 'limit' => 1,
  173. 'orderby' => 'date_created_gmt',
  174. )
  175. );
  176. $latest_note = current( $latest_notes );
  177. if ( isset( $latest_note->content ) && 1 === $approved_comments_count ) {
  178. $tooltip = wc_sanitize_tooltip( $latest_note->content );
  179. } elseif ( isset( $latest_note->content ) ) {
  180. /* translators: %d: notes count */
  181. $tooltip = wc_sanitize_tooltip( $latest_note->content . '<br/><small style="display:block">' . sprintf( _n( 'Plus %d other note', 'Plus %d other notes', ( $approved_comments_count - 1 ), 'woocommerce' ), $approved_comments_count - 1 ) . '</small>' );
  182. } else {
  183. /* translators: %d: notes count */
  184. $tooltip = wc_sanitize_tooltip( sprintf( _n( '%d note', '%d notes', $approved_comments_count, 'woocommerce' ), $approved_comments_count ) );
  185. }
  186. }
  187. if ( $tooltip ) {
  188. printf( '<mark class="order-status %s tips" data-tip="%s"><span>%s</span></mark>', esc_attr( sanitize_html_class( 'status-' . $this->object->get_status() ) ), wp_kses_post( $tooltip ), esc_html( wc_get_order_status_name( $this->object->get_status() ) ) );
  189. } else {
  190. printf( '<mark class="order-status %s"><span>%s</span></mark>', esc_attr( sanitize_html_class( 'status-' . $this->object->get_status() ) ), esc_html( wc_get_order_status_name( $this->object->get_status() ) ) );
  191. }
  192. }
  193. /**
  194. * Render columm: order_date.
  195. */
  196. protected function render_order_date_column() {
  197. $order_timestamp = $this->object->get_date_created()->getTimestamp();
  198. if ( $order_timestamp > strtotime( '-1 day', current_time( 'timestamp', true ) ) ) {
  199. $show_date = sprintf(
  200. /* translators: %s: human-readable time difference */
  201. _x( '%s ago', '%s = human-readable time difference', 'woocommerce' ),
  202. human_time_diff( $this->object->get_date_created()->getTimestamp(), current_time( 'timestamp', true ) )
  203. );
  204. } else {
  205. $show_date = $this->object->get_date_created()->date_i18n( apply_filters( 'woocommerce_admin_order_date_format', __( 'M j, Y', 'woocommerce' ) ) );
  206. }
  207. printf(
  208. '<time datetime="%1$s" title="%2$s">%3$s</time>',
  209. esc_attr( $this->object->get_date_created()->date( 'c' ) ),
  210. esc_html( $this->object->get_date_created()->date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) ),
  211. esc_html( $show_date )
  212. );
  213. }
  214. /**
  215. * Render columm: order_total.
  216. */
  217. protected function render_order_total_column() {
  218. if ( $this->object->get_payment_method_title() ) {
  219. /* translators: %s: method */
  220. echo '<span class="tips" data-tip="' . esc_attr( sprintf( __( 'via %s', 'woocommerce' ), $this->object->get_payment_method_title() ) ) . '">' . wp_kses_post( $this->object->get_formatted_order_total() ) . '</span>';
  221. } else {
  222. echo wp_kses_post( $this->object->get_formatted_order_total() );
  223. }
  224. }
  225. /**
  226. * Render columm: wc_actions.
  227. */
  228. protected function render_wc_actions_column() {
  229. echo '<p>';
  230. do_action( 'woocommerce_admin_order_actions_start', $this->object );
  231. $actions = array();
  232. if ( $this->object->has_status( array( 'pending', 'on-hold' ) ) ) {
  233. $actions['processing'] = array(
  234. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $this->object->get_id() ), 'woocommerce-mark-order-status' ),
  235. 'name' => __( 'Processing', 'woocommerce' ),
  236. 'action' => 'processing',
  237. );
  238. }
  239. if ( $this->object->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) {
  240. $actions['complete'] = array(
  241. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $this->object->get_id() ), 'woocommerce-mark-order-status' ),
  242. 'name' => __( 'Complete', 'woocommerce' ),
  243. 'action' => 'complete',
  244. );
  245. }
  246. $actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $this->object );
  247. echo wc_render_action_buttons( $actions ); // WPCS: XSS ok.
  248. do_action( 'woocommerce_admin_order_actions_end', $this->object );
  249. echo '</p>';
  250. }
  251. /**
  252. * Render columm: billing_address.
  253. */
  254. protected function render_billing_address_column() {
  255. $address = $this->object->get_formatted_billing_address();
  256. if ( $address ) {
  257. echo esc_html( preg_replace( '#<br\s*/?>#i', ', ', $address ) );
  258. if ( $this->object->get_payment_method() ) {
  259. /* translators: %s: payment method */
  260. echo '<span class="description">' . sprintf( __( 'via %s', 'woocommerce' ), esc_html( $this->object->get_payment_method_title() ) ) . '</span>'; // WPCS: XSS ok.
  261. }
  262. } else {
  263. echo '&ndash;';
  264. }
  265. }
  266. /**
  267. * Render columm: shipping_address.
  268. */
  269. protected function render_shipping_address_column() {
  270. $address = $this->object->get_formatted_shipping_address();
  271. if ( $address ) {
  272. echo '<a target="_blank" href="' . esc_url( $this->object->get_shipping_address_map_url() ) . '">' . esc_html( preg_replace( '#<br\s*/?>#i', ', ', $address ) ) . '</a>';
  273. if ( $this->object->get_shipping_method() ) {
  274. /* translators: %s: shipping method */
  275. echo '<span class="description">' . sprintf( __( 'via %s', 'woocommerce' ), esc_html( $this->object->get_shipping_method() ) ) . '</span>'; // WPCS: XSS ok.
  276. }
  277. } else {
  278. echo '&ndash;';
  279. }
  280. }
  281. /**
  282. * Template for order preview.
  283. *
  284. * @since 3.3.0
  285. */
  286. public function order_preview_template() {
  287. ?>
  288. <script type="text/template" id="tmpl-wc-modal-view-order">
  289. <div class="wc-backbone-modal wc-order-preview">
  290. <div class="wc-backbone-modal-content">
  291. <section class="wc-backbone-modal-main" role="main">
  292. <header class="wc-backbone-modal-header">
  293. <mark class="order-status status-{{ data.status }}"><span>{{ data.status_name }}</span></mark>
  294. <?php /* translators: %s: order ID */ ?>
  295. <h1><?php echo esc_html( sprintf( __( 'Order #%s', 'woocommerce' ), '{{ data.order_number }}' ) ); ?></h1>
  296. <button class="modal-close modal-close-link dashicons dashicons-no-alt">
  297. <span class="screen-reader-text"><?php esc_html_e( 'Close modal panel', 'woocommerce' ); ?></span>
  298. </button>
  299. </header>
  300. <article>
  301. <?php do_action( 'woocommerce_admin_order_preview_start' ); ?>
  302. <div class="wc-order-preview-addresses">
  303. <div class="wc-order-preview-address">
  304. <h2><?php esc_html_e( 'Billing details', 'woocommerce' ); ?></h2>
  305. {{{ data.formatted_billing_address }}}
  306. <# if ( data.data.billing.email ) { #>
  307. <strong><?php esc_html_e( 'Email', 'woocommerce' ); ?></strong>
  308. <a href="mailto:{{ data.data.billing.email }}">{{ data.data.billing.email }}</a>
  309. <# } #>
  310. <# if ( data.data.billing.phone ) { #>
  311. <strong><?php esc_html_e( 'Phone', 'woocommerce' ); ?></strong>
  312. <a href="tel:{{ data.data.billing.phone }}">{{ data.data.billing.phone }}</a>
  313. <# } #>
  314. <# if ( data.payment_via ) { #>
  315. <strong><?php esc_html_e( 'Payment via', 'woocommerce' ); ?></strong>
  316. {{{ data.payment_via }}}
  317. <# } #>
  318. </div>
  319. <# if ( data.needs_shipping ) { #>
  320. <div class="wc-order-preview-address">
  321. <h2><?php esc_html_e( 'Shipping details', 'woocommerce' ); ?></h2>
  322. <# if ( data.ship_to_billing ) { #>
  323. {{{ data.formatted_billing_address }}}
  324. <# } else { #>
  325. <a href="{{ data.shipping_address_map_url }}" target="_blank">{{{ data.formatted_shipping_address }}}</a>
  326. <# } #>
  327. <# if ( data.data.customer_note ) { #>
  328. <strong><?php esc_html_e( 'Note', 'woocommerce' ); ?></strong>
  329. {{ data.data.customer_note }}
  330. <# } #>
  331. <# if ( data.shipping_via ) { #>
  332. <strong><?php esc_html_e( 'Shipping method', 'woocommerce' ); ?></strong>
  333. {{ data.shipping_via }}
  334. <# } #>
  335. </div>
  336. <# } #>
  337. </div>
  338. {{{ data.item_html }}}
  339. <?php do_action( 'woocommerce_admin_order_preview_end' ); ?>
  340. </article>
  341. <footer>
  342. <div class="inner">
  343. {{{ data.actions_html }}}
  344. <a class="button button-primary button-large" href="<?php echo esc_url( admin_url( 'post.php?action=edit' ) ); ?>&post={{ data.data.id }}"><?php esc_html_e( 'Edit order', 'woocommerce' ); ?></a>
  345. </div>
  346. </footer>
  347. </section>
  348. </div>
  349. </div>
  350. <div class="wc-backbone-modal-backdrop modal-close"></div>
  351. </script>
  352. <?php
  353. }
  354. /**
  355. * Get items to display in the preview as HTML.
  356. *
  357. * @param WC_Order $order Order object.
  358. * @return string
  359. */
  360. public static function get_order_preview_item_html( $order ) {
  361. $hidden_order_itemmeta = apply_filters(
  362. 'woocommerce_hidden_order_itemmeta', array(
  363. '_qty',
  364. '_tax_class',
  365. '_product_id',
  366. '_variation_id',
  367. '_line_subtotal',
  368. '_line_subtotal_tax',
  369. '_line_total',
  370. '_line_tax',
  371. 'method_id',
  372. 'cost',
  373. )
  374. );
  375. $line_items = apply_filters( 'woocommerce_admin_order_preview_line_items', $order->get_items(), $order );
  376. $columns = apply_filters(
  377. 'woocommerce_admin_order_preview_line_item_columns', array(
  378. 'product' => __( 'Product', 'woocommerce' ),
  379. 'quantity' => __( 'Quantity', 'woocommerce' ),
  380. 'tax' => __( 'Tax', 'woocommerce' ),
  381. 'total' => __( 'Total', 'woocommerce' ),
  382. ), $order
  383. );
  384. if ( ! wc_tax_enabled() ) {
  385. unset( $columns['tax'] );
  386. }
  387. $html = '
  388. <div class="wc-order-preview-table-wrapper">
  389. <table cellspacing="0" class="wc-order-preview-table">
  390. <thead>
  391. <tr>';
  392. foreach ( $columns as $column => $label ) {
  393. $html .= '<th class="wc-order-preview-table__column--' . esc_attr( $column ) . '">' . esc_html( $label ) . '</th>';
  394. }
  395. $html .= '
  396. </tr>
  397. </thead>
  398. <tbody>';
  399. foreach ( $line_items as $item_id => $item ) {
  400. $product_object = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : null;
  401. $row_class = apply_filters( 'woocommerce_admin_html_order_preview_item_class', '', $item, $order );
  402. $html .= '<tr class="wc-order-preview-table__item wc-order-preview-table__item--' . esc_attr( $item_id ) . ( $row_class ? ' ' . esc_attr( $row_class ) : '' ) . '">';
  403. foreach ( $columns as $column => $label ) {
  404. $html .= '<td class="wc-order-preview-table__column--' . esc_attr( $column ) . '">';
  405. switch ( $column ) {
  406. case 'product':
  407. $html .= wp_kses_post( $item->get_name() );
  408. if ( $product_object ) {
  409. $html .= '<div class="wc-order-item-sku">' . esc_html( $product_object->get_sku() ) . '</div>';
  410. }
  411. $meta_data = $item->get_formatted_meta_data( '' );
  412. if ( $meta_data ) {
  413. $html .= '<table cellspacing="0" class="wc-order-item-meta">';
  414. foreach ( $meta_data as $meta_id => $meta ) {
  415. if ( in_array( $meta->key, $hidden_order_itemmeta ) ) {
  416. continue;
  417. }
  418. $html .= '<tr><th>' . wp_kses_post( $meta->display_key ) . ':</th><td>' . wp_kses_post( force_balance_tags( $meta->display_value ) ) . '</td></tr>';
  419. }
  420. $html .= '</table>';
  421. }
  422. break;
  423. case 'quantity':
  424. $html .= esc_html( $item->get_quantity() );
  425. break;
  426. case 'tax':
  427. $html .= wc_price( $item->get_total_tax(), array( 'currency' => $order->get_currency() ) );
  428. break;
  429. case 'total':
  430. $html .= wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) );
  431. break;
  432. default:
  433. $html .= apply_filters( 'woocommerce_admin_order_preview_line_item_column_' . sanitize_key( $column ), '', $item, $item_id, $order );
  434. break;
  435. }
  436. $html .= '</td>';
  437. }
  438. $html .= '</tr>';
  439. }
  440. $html .= '
  441. </tbody>
  442. </table>
  443. </div>';
  444. return $html;
  445. }
  446. /**
  447. * Get actions to display in the preview as HTML.
  448. *
  449. * @param WC_Order $order Order object.
  450. * @return string
  451. */
  452. public static function get_order_preview_actions_html( $order ) {
  453. $actions = array();
  454. $status_actions = array();
  455. if ( $order->has_status( array( 'pending' ) ) ) {
  456. $status_actions['on-hold'] = array(
  457. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=on-hold&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
  458. 'name' => __( 'On-hold', 'woocommerce' ),
  459. 'action' => 'on-hold',
  460. );
  461. }
  462. if ( $order->has_status( array( 'pending', 'on-hold' ) ) ) {
  463. $status_actions['processing'] = array(
  464. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
  465. 'name' => __( 'Processing', 'woocommerce' ),
  466. 'action' => 'processing',
  467. );
  468. }
  469. if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) {
  470. $status_actions['complete'] = array(
  471. 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ),
  472. 'name' => __( 'Completed', 'woocommerce' ),
  473. 'action' => 'complete',
  474. );
  475. }
  476. if ( $status_actions ) {
  477. $actions['status'] = array(
  478. 'group' => __( 'Change status: ', 'woocommerce' ),
  479. 'actions' => $status_actions,
  480. );
  481. }
  482. return wc_render_action_buttons( apply_filters( 'woocommerce_admin_order_preview_actions', $actions, $order ) );
  483. }
  484. /**
  485. * Get order details to send to the ajax endpoint for previews.
  486. *
  487. * @param WC_Order $order Order object.
  488. * @return array
  489. */
  490. public static function order_preview_get_order_details( $order ) {
  491. if ( ! $order ) {
  492. return array();
  493. }
  494. $payment_via = $order->get_payment_method_title();
  495. $payment_method = $order->get_payment_method();
  496. $payment_gateways = WC()->payment_gateways() ? WC()->payment_gateways->payment_gateways() : array();
  497. $transaction_id = $order->get_transaction_id();
  498. if ( $transaction_id ) {
  499. $url = isset( $payment_gateways[ $payment_method ] ) ? $payment_gateways[ $payment_method ]->get_transaction_url( $order ) : false;
  500. if ( $url ) {
  501. $payment_via .= ' (<a href="' . esc_url( $url ) . '" target="_blank">' . esc_html( $transaction_id ) . '</a>)';
  502. } else {
  503. $payment_via .= ' (' . esc_html( $transaction_id ) . ')';
  504. }
  505. }
  506. $billing_address = $order->get_formatted_billing_address();
  507. $shipping_address = $order->get_formatted_shipping_address();
  508. return apply_filters(
  509. 'woocommerce_admin_order_preview_get_order_details', array(
  510. 'data' => $order->get_data(),
  511. 'order_number' => $order->get_order_number(),
  512. 'item_html' => WC_Admin_List_Table_Orders::get_order_preview_item_html( $order ),
  513. 'actions_html' => WC_Admin_List_Table_Orders::get_order_preview_actions_html( $order ),
  514. 'ship_to_billing' => wc_ship_to_billing_address_only(),
  515. 'needs_shipping' => $order->needs_shipping_address(),
  516. 'formatted_billing_address' => $billing_address ? $billing_address : __( 'N/A', 'woocommerce' ),
  517. 'formatted_shipping_address' => $shipping_address ? $shipping_address : __( 'N/A', 'woocommerce' ),
  518. 'shipping_address_map_url' => $order->get_shipping_address_map_url(),
  519. 'payment_via' => $payment_via,
  520. 'shipping_via' => $order->get_shipping_method(),
  521. 'status' => $order->get_status(),
  522. 'status_name' => wc_get_order_status_name( $order->get_status() ),
  523. ), $order
  524. );
  525. }
  526. /**
  527. * Handle bulk actions.
  528. *
  529. * @param string $redirect_to URL to redirect to.
  530. * @param string $action Action name.
  531. * @param array $ids List of ids.
  532. * @return string
  533. */
  534. public function handle_bulk_actions( $redirect_to, $action, $ids ) {
  535. // Bail out if this is not a status-changing action.
  536. if ( false === strpos( $action, 'mark_' ) ) {
  537. return $redirect_to;
  538. }
  539. $order_statuses = wc_get_order_statuses();
  540. $new_status = substr( $action, 5 ); // Get the status name from action.
  541. $report_action = 'marked_' . $new_status;
  542. // Sanity check: bail out if this is actually not a status, or is
  543. // not a registered status.
  544. if ( ! isset( $order_statuses[ 'wc-' . $new_status ] ) ) {
  545. return $redirect_to;
  546. }
  547. $changed = 0;
  548. $ids = array_map( 'absint', $ids );
  549. foreach ( $ids as $id ) {
  550. $order = wc_get_order( $id );
  551. $order->update_status( $new_status, __( 'Order status changed by bulk edit:', 'woocommerce' ), true );
  552. do_action( 'woocommerce_order_edit_status', $id, $new_status );
  553. $changed++;
  554. }
  555. $redirect_to = add_query_arg(
  556. array(
  557. 'post_type' => $this->list_table_type,
  558. $report_action => true,
  559. 'changed' => $changed,
  560. 'ids' => join( ',', $ids ),
  561. ), $redirect_to
  562. );
  563. return esc_url_raw( $redirect_to );
  564. }
  565. /**
  566. * Show confirmation message that order status changed for number of orders.
  567. */
  568. public function bulk_admin_notices() {
  569. global $post_type, $pagenow;
  570. // Bail out if not on shop order list page.
  571. if ( 'edit.php' !== $pagenow || 'shop_order' !== $post_type ) {
  572. return;
  573. }
  574. $order_statuses = wc_get_order_statuses();
  575. // Check if any status changes happened.
  576. foreach ( $order_statuses as $slug => $name ) {
  577. if ( isset( $_REQUEST[ 'marked_' . str_replace( 'wc-', '', $slug ) ] ) ) { // WPCS: input var ok.
  578. $number = isset( $_REQUEST['changed'] ) ? absint( $_REQUEST['changed'] ) : 0; // WPCS: input var ok.
  579. /* translators: %s: orders count */
  580. $message = sprintf( _n( '%d order status changed.', '%d order statuses changed.', $number, 'woocommerce' ), number_format_i18n( $number ) );
  581. echo '<div class="updated"><p>' . esc_html( $message ) . '</p></div>';
  582. break;
  583. }
  584. }
  585. }
  586. /**
  587. * See if we should render search filters or not.
  588. */
  589. public function restrict_manage_posts() {
  590. global $typenow;
  591. if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ), true ) ) {
  592. $this->render_filters();
  593. }
  594. }
  595. /**
  596. * Render any custom filters and search inputs for the list table.
  597. */
  598. protected function render_filters() {
  599. $user_string = '';
  600. $user_id = '';
  601. if ( ! empty( $_GET['_customer_user'] ) ) { // WPCS: input var ok.
  602. $user_id = absint( $_GET['_customer_user'] ); // WPCS: input var ok, sanitization ok.
  603. $user = get_user_by( 'id', $user_id );
  604. $user_string = sprintf(
  605. /* translators: 1: user display name 2: user ID 3: user email */
  606. esc_html__( '%1$s (#%2$s &ndash; %3$s)', 'woocommerce' ),
  607. $user->display_name,
  608. absint( $user->ID ),
  609. $user->user_email
  610. );
  611. }
  612. ?>
  613. <select class="wc-customer-search" name="_customer_user" data-placeholder="<?php esc_attr_e( 'Search for a customer&hellip;', 'woocommerce' ); ?>" data-allow_clear="true">
  614. <option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo wp_kses_post( $user_string ); ?><option>
  615. </select>
  616. <?php
  617. }
  618. /**
  619. * Handle any filters.
  620. *
  621. * @param array $query_vars Query vars.
  622. * @return array
  623. */
  624. public function request_query( $query_vars ) {
  625. global $typenow;
  626. if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ), true ) ) {
  627. return $this->query_filters( $query_vars );
  628. }
  629. return $query_vars;
  630. }
  631. /**
  632. * Handle any custom filters.
  633. *
  634. * @param array $query_vars Query vars.
  635. * @return array
  636. */
  637. protected function query_filters( $query_vars ) {
  638. global $wp_post_statuses;
  639. // Filter the orders by the posted customer.
  640. if ( ! empty( $_GET['_customer_user'] ) ) { // WPCS: input var ok.
  641. // @codingStandardsIgnoreStart
  642. $query_vars['meta_query'] = array(
  643. array(
  644. 'key' => '_customer_user',
  645. 'value' => (int) $_GET['_customer_user'], // WPCS: input var ok, sanitization ok.
  646. 'compare' => '=',
  647. ),
  648. );
  649. // @codingStandardsIgnoreEnd
  650. }
  651. // Sorting.
  652. if ( isset( $query_vars['orderby'] ) ) {
  653. if ( 'order_total' === $query_vars['orderby'] ) {
  654. // @codingStandardsIgnoreStart
  655. $query_vars = array_merge( $query_vars, array(
  656. 'meta_key' => '_order_total',
  657. 'orderby' => 'meta_value_num',
  658. ) );
  659. // @codingStandardsIgnoreEnd
  660. }
  661. }
  662. // Status.
  663. if ( ! isset( $query_vars['post_status'] ) ) {
  664. $post_statuses = wc_get_order_statuses();
  665. foreach ( $post_statuses as $status => $value ) {
  666. if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
  667. unset( $post_statuses[ $status ] );
  668. }
  669. }
  670. $query_vars['post_status'] = array_keys( $post_statuses );
  671. }
  672. return $query_vars;
  673. }
  674. /**
  675. * Change the label when searching orders.
  676. *
  677. * @param mixed $query Current search query.
  678. * @return string
  679. */
  680. public function search_label( $query ) {
  681. global $pagenow, $typenow;
  682. if ( 'edit.php' !== $pagenow || 'shop_order' !== $typenow || ! get_query_var( 'shop_order_search' ) || ! isset( $_GET['s'] ) ) { // WPCS: input var ok.
  683. return $query;
  684. }
  685. return wc_clean( wp_unslash( $_GET['s'] ) ); // WPCS: input var ok, sanitization ok.
  686. }
  687. /**
  688. * Query vars for custom searches.
  689. *
  690. * @param mixed $public_query_vars Array of query vars.
  691. * @return array
  692. */
  693. public function add_custom_query_var( $public_query_vars ) {
  694. $public_query_vars[] = 'shop_order_search';
  695. return $public_query_vars;
  696. }
  697. /**
  698. * Search custom fields as well as content.
  699. *
  700. * @param WP_Query $wp Query object.
  701. */
  702. public function search_custom_fields( $wp ) {
  703. global $pagenow;
  704. if ( 'edit.php' !== $pagenow || empty( $wp->query_vars['s'] ) || 'shop_order' !== $wp->query_vars['post_type'] || ! isset( $_GET['s'] ) ) { // WPCS: input var ok.
  705. return;
  706. }
  707. $post_ids = wc_order_search( wc_clean( wp_unslash( $_GET['s'] ) ) ); // WPCS: input var ok, sanitization ok.
  708. if ( ! empty( $post_ids ) ) {
  709. // Remove "s" - we don't want to search order name.
  710. unset( $wp->query_vars['s'] );
  711. // so we know we're doing this.
  712. $wp->query_vars['shop_order_search'] = true;
  713. // Search by found posts.
  714. $wp->query_vars['post__in'] = array_merge( $post_ids, array( 0 ) );
  715. }
  716. }
  717. }