Skip to content

Commit 170a38e

Browse files
committed
Add order filters
1 parent fca3599 commit 170a38e

File tree

1 file changed

+144
-4
lines changed

1 file changed

+144
-4
lines changed

includes/Admin/Orders.php

Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,175 @@
22

33
namespace WCPOS\WooCommercePOS\Admin;
44

5+
use WC_Order;
6+
use WP_Query;
7+
use const WCPOS\WooCommercePOS\PLUGIN_URL;
8+
59
class Orders {
610

711
public function __construct() {
812
add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hidden_order_itemmeta' ) );
913
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+
}
1124

1225
/**
1326
* Hides uuid from appearing on Order Edit page
1427
*
1528
* @param array $meta_keys
1629
* @return array
1730
*/
18-
public function hidden_order_itemmeta( array $meta_keys ) {
31+
public function hidden_order_itemmeta( array $meta_keys ): array {
1932
return array_merge( $meta_keys, array( '_woocommerce_pos_uuid', '_woocommerce_pos_tax_status' ) );
2033
}
2134

2235
/**
2336
* Makes POS orders editable by default
2437
*
2538
* @param bool $is_editable
26-
* @param \WC_Order $order
39+
* @param WC_Order $order
2740
* @return bool
2841
*/
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 {
3043
if ( $order->get_status() == 'pos-open' ) {
3144
$is_editable = true;
3245
}
3346
return $is_editable;
3447
}
3548

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+
}
36176
}

0 commit comments

Comments
 (0)