|
2 | 2 |
|
3 | 3 | namespace WCPOS\WooCommercePOS\Admin; |
4 | 4 |
|
| 5 | +use WC_Order; |
| 6 | +use WP_Query; |
| 7 | +use const WCPOS\WooCommercePOS\PLUGIN_URL; |
| 8 | + |
5 | 9 | class Orders { |
6 | 10 |
|
7 | 11 | public function __construct() { |
8 | 12 | add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hidden_order_itemmeta' ) ); |
9 | 13 | add_filter( 'wc_order_is_editable', array( $this, 'wc_order_is_editable' ), 10, 2 ); |
10 | | - } |
| 14 | + |
| 15 | + // add filter dropdown to orders list page |
| 16 | + add_action( 'restrict_manage_posts', array( $this, 'order_filter_dropdown' ) ); |
| 17 | + add_filter( 'parse_query', array( $this, 'parse_query' ) ); |
| 18 | + |
| 19 | + // add column for POS orders |
| 20 | + add_filter( 'manage_edit-shop_order_columns', array( $this, 'pos_shop_order_column' ) ); |
| 21 | + add_action( 'manage_shop_order_posts_custom_column', array( $this, 'pos_orders_list_column_content' ), 10, 2 ); |
| 22 | + add_action( 'admin_enqueue_scripts', array( $this, 'pos_order_column_width' ) ); |
| 23 | + } |
11 | 24 |
|
12 | 25 | /** |
13 | 26 | * Hides uuid from appearing on Order Edit page |
14 | 27 | * |
15 | 28 | * @param array $meta_keys |
16 | 29 | * @return array |
17 | 30 | */ |
18 | | - public function hidden_order_itemmeta( array $meta_keys ) { |
| 31 | + public function hidden_order_itemmeta( array $meta_keys ): array { |
19 | 32 | return array_merge( $meta_keys, array( '_woocommerce_pos_uuid', '_woocommerce_pos_tax_status' ) ); |
20 | 33 | } |
21 | 34 |
|
22 | 35 | /** |
23 | 36 | * Makes POS orders editable by default |
24 | 37 | * |
25 | 38 | * @param bool $is_editable |
26 | | - * @param \WC_Order $order |
| 39 | + * @param WC_Order $order |
27 | 40 | * @return bool |
28 | 41 | */ |
29 | | - public function wc_order_is_editable( bool $is_editable, \WC_Order $order ) { |
| 42 | + public function wc_order_is_editable( bool $is_editable, WC_Order $order ): bool { |
30 | 43 | if ( $order->get_status() == 'pos-open' ) { |
31 | 44 | $is_editable = true; |
32 | 45 | } |
33 | 46 | return $is_editable; |
34 | 47 | } |
35 | 48 |
|
| 49 | + public function order_filter_dropdown() { |
| 50 | + $selected = isset( $_GET['pos_order'] ) ? $_GET['pos_order'] : ''; |
| 51 | + |
| 52 | + $options = array( |
| 53 | + 'yes' => __( 'POS', 'woocommerce-pos' ), |
| 54 | + 'no' => __( 'Online', 'woocommerce-pos' ), |
| 55 | + ); |
| 56 | + |
| 57 | + echo '<select name="pos_order" id="pos_order">'; |
| 58 | + echo '<option value="">All orders</option>'; |
| 59 | + foreach ( $options as $value => $label ) { |
| 60 | + echo '<option value="' . esc_attr( $value ) . '" '; |
| 61 | + selected( $selected, $value ); |
| 62 | + echo '>' . esc_html( $label ) . '</option>'; |
| 63 | + } |
| 64 | + echo '</select>'; |
| 65 | + } |
| 66 | + |
| 67 | + public function parse_query( WP_Query $query ) { |
| 68 | + if ( isset( $_GET['pos_order'] ) && $_GET['pos_order'] != '' ) { |
| 69 | + $meta_query = array( 'relation' => 'AND' ); |
| 70 | + |
| 71 | + if ( $_GET['pos_order'] == 'yes' ) { |
| 72 | + $meta_query[] = array( |
| 73 | + 'relation' => 'OR', |
| 74 | + array( |
| 75 | + 'key' => '_pos', |
| 76 | + 'value' => '1', |
| 77 | + 'compare' => '=', |
| 78 | + ), |
| 79 | + array( |
| 80 | + 'key' => '_created_via', |
| 81 | + 'value' => 'woocommerce-pos', |
| 82 | + 'compare' => '=', |
| 83 | + ), |
| 84 | + ); |
| 85 | + } elseif ( $_GET['pos_order'] == 'no' ) { |
| 86 | + $meta_query[] = array( |
| 87 | + 'relation' => 'OR', |
| 88 | + array( |
| 89 | + 'key' => '_pos', |
| 90 | + 'compare' => 'NOT EXISTS', |
| 91 | + ), |
| 92 | + array( |
| 93 | + 'key' => '_pos', |
| 94 | + 'value' => '1', |
| 95 | + 'compare' => '!=', |
| 96 | + ), |
| 97 | + ); |
| 98 | + $meta_query[] = array( |
| 99 | + 'relation' => 'OR', |
| 100 | + array( |
| 101 | + 'key' => '_created_via', |
| 102 | + 'compare' => 'NOT EXISTS', |
| 103 | + ), |
| 104 | + array( |
| 105 | + 'key' => '_created_via', |
| 106 | + 'value' => 'woocommerce-pos', |
| 107 | + 'compare' => '!=', |
| 108 | + ), |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + if ( isset( $query->query_vars['meta_query'] ) ) { |
| 113 | + $query->query_vars['meta_query'] = array_merge( |
| 114 | + $query->query_vars['meta_query'], |
| 115 | + $meta_query |
| 116 | + ); |
| 117 | + } else { |
| 118 | + $query->query_vars['meta_query'] = $meta_query; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /** |
| 125 | + * @param string[] $columns The column header labels keyed by column ID. |
| 126 | + * @return string[] |
| 127 | + */ |
| 128 | + public function pos_shop_order_column( array $columns ): array { |
| 129 | + $new_columns = array(); |
| 130 | + |
| 131 | + foreach ( $columns as $column_name => $column_info ) { |
| 132 | + |
| 133 | + if ( 'order_date' === $column_name ) { |
| 134 | + $new_columns['wcpos'] = ''; |
| 135 | + } |
| 136 | + |
| 137 | + $new_columns[ $column_name ] = $column_info; |
| 138 | + } |
| 139 | + |
| 140 | + return $new_columns; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * |
| 145 | + * |
| 146 | + * @param string $column_name The name of the column to display. |
| 147 | + * @param int $post_id The current post ID. |
| 148 | + * |
| 149 | + * @return void |
| 150 | + */ |
| 151 | + public function pos_orders_list_column_content( string $column_name, int $post_id ): void { |
| 152 | + global $post; |
| 153 | + if ( $column_name == 'wcpos' ) { |
| 154 | + $legacy = get_post_meta( $post->ID, '_pos', true ); |
| 155 | + $created_via = get_post_meta( $post->ID, '_created_via', true ); |
| 156 | + if ( $created_via == 'woocommerce-pos' || $legacy == 1 ) { |
| 157 | + echo '<span class="wcpos-icon"></span>'; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @return void |
| 164 | + */ |
| 165 | + public function pos_order_column_width() { |
| 166 | + // Define the URL of your custom icon |
| 167 | + $icon_url = PLUGIN_URL . '/assets/img/wp-menu-icon.svg'; |
| 168 | + |
| 169 | + $css = ' |
| 170 | + body.post-type-shop_order .wp-list-table .column-wcpos { width: 16px !important; padding: 0 !important; } |
| 171 | + .wcpos-icon { display: inline-block; width: 16px; height: 16px; background: url(' . $icon_url . ') no-repeat center center; margin-top: 10px; } |
| 172 | + '; |
| 173 | + |
| 174 | + wp_add_inline_style( 'woocommerce_admin_styles', $css ); |
| 175 | + } |
36 | 176 | } |
0 commit comments