From bde166db3d07b9ef2a0697f0d3002c19d4b80925 Mon Sep 17 00:00:00 2001 From: barryhughes <3594411+barryhughes@users.noreply.github.com> Date: Wed, 4 May 2022 22:32:38 +0000 Subject: [PATCH] Add table views. --- .../src/Internal/Admin/Orders/ListTable.php | 85 +++++++++++++++++-- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php b/plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php index cbce678cf8e..091ed4bea33 100644 --- a/plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php +++ b/plugins/woocommerce/src/Internal/Admin/Orders/ListTable.php @@ -78,23 +78,26 @@ class ListTable extends WP_List_Table {

{$title}

{$add_new} +
"; $this->views(); + + echo '
'; + $this->search_box( esc_html__( 'Search orders', 'woocommerce' ), 'orders-search-input' ); + parent::display(); - echo '
'; + echo ' '; } /** * Prepares the list of items for displaying. */ public function prepare_items() { - $status = sanitize_text_field( wp_unslash( $_REQUEST['status'] ?? '' ) ); - $search = sanitize_text_field( wp_unslash( $_REQUEST['s'] ?? '' ) ); - $args = array( - 'limit' => $this->get_items_per_page( 'edit_orders_per_page' ), - 'page' => $this->get_pagenum(), + 'limit' => $this->get_items_per_page( 'edit_orders_per_page' ), + 'page' => $this->get_pagenum(), + 'status' => sanitize_text_field( wp_unslash( $_REQUEST['status'] ?? '' ) ), ); // @todo Confirm this is the best way to query. @@ -103,6 +106,76 @@ class ListTable extends WP_List_Table { $this->items = wc_get_orders( $args ); } + /** + * Get the list of views for this table (all orders, completed orders, etc, each with a count of the number of + * corresponding orders). + * + * @return array + */ + public function get_views() { + $view_counts = array(); + $view_links = array(); + $statuses = wc_get_order_statuses(); + $current = isset( $_GET['status'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['status'] ?? '' ) ) : 'all'; + + foreach ( $statuses as $slug => $name ) { + $total_in_status = $this->count_orders_by_status( $slug ); + + if ( $total_in_status > 0 ) { + $view_counts[ $slug ] = $total_in_status; + } + } + + $all_count = array_sum( $view_counts ); + $view_links['all'] = $this->get_view_link( 'all', __( 'All', 'woocommerce' ), $all_count, 'all' === $current ); + + foreach ( $view_counts as $slug => $count ) { + $view_links[ $slug ] = $this->get_view_link( $slug, $statuses[ $slug ], $count, $slug === $current ); + } + + return $view_links; + } + + /** + * Count orders by status. + * + * @todo review and replace (probably not ideal to do this work here). + * + * @param string $status The order status we are interested in. + * + * @return int + */ + private function count_orders_by_status( string $status ): int { + $orders = wc_get_orders( + array( + 'limit' => -1, + 'return' => 'ids', + 'status' => $status, + ) + ); + + return count( $orders ); + } + + /** + * Form a link to use in the list of table views. + * + * @param string $slug Slug used to identify the view (usually the order status slug). + * @param string $name Human-readable name of the view (usually the order status label). + * @param int $count Number of items in this view. + * @param bool $current If this is the current view. + * + * @return string + */ + private function get_view_link( string $slug, string $name, int $count, bool $current ): string { + $url = esc_url( add_query_arg( 'status', $slug, get_admin_url( null, 'admin.php?page=wc-orders' ) ) ); + $name = esc_html( $name ); + $count = absint( $count ); + $class = $current ? 'class="current"' : ''; + + return "$name ($count)"; + } + /** * Get list columns. *