forked from bekarice/woocommerce-filter-orders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-advanced-order-filters.php
400 lines (338 loc) · 11.2 KB
/
woocommerce-advanced-order-filters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* Plugin Name: WooCommerce Advanced Order Filters
* Plugin URI: http://www.skyverge.com/product/woocommerce-filter-orders/
* Description: Adds the ability to filter orders by attributes, shipping method, or coupon used.
* Author: Lukas Besch
* Author URI: https://lukasbesch.com/
* Version: 1.2.0
* Text Domain: wc-advanced-order-filters
*
* GitHub Plugin URI: lukasbesch/woocommerce-advanced-order-filters/
* GitHub Branch: master
*
* Copyright: (c) 2019 Lukas Besch ([email protected])
* Copyright: (c) 2015-2017 SkyVerge, Inc. ([email protected])
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package WC-Advanced-Order-Filters
* @author Lukas Besch
* @category Admin
* @copyright Copyright (c) 2015-2017, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*
*/
defined( 'ABSPATH' ) or exit;
// fire it up!
add_action( 'plugins_loaded', 'wc_advanced_order_filters');
/**
* Plugin Description
*
* Adds custom filtering to the orders screen.
*/
class WC_Advanced_Order_Filters {
const VERSION = '1.2.0';
/** @var WC_Advanced_Order_Filters single instance of this plugin */
protected static $instance;
/** @var string The Attribute slug */
public $filterableAttribute;
/**
* WC_Filter_Orders constructor.
*
* @since 1.0.0
*/
public function __construct() {
// load translations
add_action( 'init', array( $this, 'load_translation' ) );
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
$this->filterableAttribute = 'pa_popup-store';
// adds the additional filtering dropdowns to the orders page
add_action( 'restrict_manage_posts', array( $this, 'filter_orders_by_coupon_used' ), 20 );
add_action( 'restrict_manage_posts', array( $this, 'filter_orders_by_shipping_method_used' ), 20 );
add_action( 'restrict_manage_posts', array( $this, 'filter_orders_by_attribute' ), 20 );
add_action( 'restrict_manage_posts', array( $this, 'filter_orders_by_category' ), 20 );
// makes filterable
add_filter( 'posts_join', array( $this, 'add_order_items_join' ) );
add_filter( 'posts_where', array( $this, 'add_filterable_where' ) );
}
}
/** Plugin methods ***************************************/
/**
* Adds the coupon filtering dropdown to the orders list
*
* @since 1.0.0
*/
public function filter_orders_by_coupon_used() {
if ( ! $this->is_orders_page() ) {
return;
}
$args = array(
'posts_per_page' => - 1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
);
$coupons = get_posts( $args );
if ( empty( $coupons ) ) {
return;
}
$this->select_html('coupon', $coupons, '_coupons_used', 'post_title');
}
/**
* Adds the shipping method filtering dropdown to the orders list
*
* @since 1.2.0
*/
public function filter_orders_by_shipping_method_used()
{
if (!$this->is_orders_page()) {
return;
}
$shipping_methods = WC()->shipping->get_shipping_methods();
if (empty($shipping_methods)) {
return;
}
$this->select_html('shipping method', $shipping_methods, '_shipping_method_used', 'method_title');
}
/**
* Adds the attribute filtering dropdown to the orders list
*
* @since 1.2.0
*/
public function filter_orders_by_attribute() {
if ( ! $this->is_orders_page() ) {
return;
}
$attributes = get_terms( [
'taxonomy' => $this->filterableAttribute
] );
if ( empty( $attributes ) ) {
return;
}
?><input type="hidden" name="_attribute_name" value="<?php esc_attr_e( $this->filterableAttribute ); ?>"><?php
$this->select_html('attribute', $attributes, '_attributes_used', 'slug', 'name');
}
/**
* Adds the category filtering dropdown to the orders list
*
* @since 1.2.0
*/
public function filter_orders_by_category() {
if ( ! $this->is_orders_page() ) {
return;
}
$categories = get_terms( [
'taxonomy' => 'product_cat'
] );
if ( empty( $categories ) ) {
return;
}
$this->select_html('category', $categories, '_category', 'term_id', 'name');
$excluded_checked = isset( $_GET['_category_exclude'] ) ? checked( $_GET['_category_exclude'], $_GET['_category_exclude'], false ) : '';
?>
<label for="_category_exclude" style="float: left; margin: 6px 6px 6px 0">
<input type="checkbox" name="_category_exclude" <?php echo $excluded_checked ?> style="height: 16px">
<?php
printf( esc_html__( 'Exclude category', 'wc-advanced-order-filters' ) );
?>
</label>
<?php
}
/**
* Print the option for the filter dropdown.
*
* @param $item
* @param $getKey
* @param $valueKey
* @param $labelKey
*/
public function option_html($item, $getKey, $valueKey, $labelKey = false) {
$labelKey = !empty($labelKey) ? $labelKey : $valueKey;
$selected = isset( $_GET[$getKey] ) ? selected( $item->{$valueKey}, $_GET[$getKey], false ) : '';
?>
<option value="<?php esc_attr_e( $item->{$valueKey} ); ?>" <?php esc_attr_e( $selected ); ?>>
<?php echo esc_html( $item->{$labelKey} ); ?>
</option>
<?php
}
/**
* Print the select for the filter dropdown.
*
* @param $name
* @param $items
* @param $getKey
* @param $valueKey
* @param bool $labelKey
*/
public function select_html($name, $items, $getKey, $valueKey, $labelKey = false) {
$labelKey = !empty($labelKey) ? $labelKey : $valueKey;
?>
<select name="<?php esc_attr_e( $getKey ) ?>" id="<?php esc_attr_e( 'dropdown' . $getKey ) ?>">
<option value="">
<?php
/* translators: dropdown placeholder on orders admin screen */
printf( esc_html__( 'Filter by %d', 'wc-advanced-order-filters' ), $name );
?>
</option>
<?php
foreach ( $items as $item ) {
$this->option_html($item, $getKey, $valueKey, $labelKey);
}
?>
</select>
<?php
}
/**
* Modify SQL JOIN for filtering the orders
*
* @since 1.0.0
*
* @param string $join JOIN part of the sql query
* @return string $join modified JOIN part of sql query
*/
public function add_order_items_join( $join ) {
global $wpdb;
if ( ! $this->is_orders_page() ) {
return $join;
}
if ( ( isset( $_GET['_coupons_used'] ) && ! empty( $_GET['_coupons_used'] ) )
|| (isset( $_GET['_shipping_method_used'] ) && ! empty( $_GET['_shipping_method_used'] ) )
|| (isset( $_GET['_attributes_used'] ) && ! empty( $_GET['_attributes_used'] ) )
) {
$join .= " LEFT JOIN {$wpdb->prefix}woocommerce_order_items woi ON {$wpdb->posts}.ID = woi.order_id";
}
if ( isset( $_GET['_attributes_used'] ) && ! empty( $_GET['_attributes_used'] ) ) {
$join .= " LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta woim ON woi.order_item_id = woim.order_item_id";
}
return $join;
}
/**
* Modify SQL WHERE for filtering the orders
*
* @since 1.0.0
*
* @param string $where WHERE part of the sql query
* @return string $where modified WHERE part of sql query
*/
public function add_filterable_where( $where ) {
global $wpdb;
if ( ! $this->is_orders_page() ) {
return $where;
}
// Main WHERE query part
if ( isset( $_GET['_coupons_used'] ) && ! empty( $_GET['_coupons_used'] ) ) {
$where .= $wpdb->prepare( " AND woi.order_item_type='coupon' AND woi.order_item_name='%s'", wc_clean( $_GET['_coupons_used'] ) );
}
if ( isset( $_GET['_shipping_method_used'] ) && ! empty( $_GET['_shipping_method_used'] ) ) {
$where .= $wpdb->prepare( " AND woi.order_item_type='shipping' AND woi.order_item_name='%s'", wc_clean( $_GET['_shipping_method_used'] ) );
}
if ( isset( $_GET['_attributes_used'], $_GET['_attribute_name'] ) && ! empty( $_GET['_attributes_used'] ) && ! empty( $_GET['_attribute_name'] ) ) {
$where .= $wpdb->prepare( " AND woim.meta_key='%s' AND woim.meta_value='%s'", wc_clean( $_GET['_attribute_name'] ), wc_clean( $_GET['_attributes_used'] ) );
}
if ( isset( $_GET['_category'] ) && ! empty( $_GET['_category'] ) ) {
$operator = 'IN';
if ( isset( $_GET['_category_exclude'] ) && $_GET['_category_exclude'] === 'on' ) {
$operator = 'NOT IN';
}
$where .= " AND {$wpdb->posts}.ID $operator ({$this->query_order_products($_GET['_category'])})";
}
return $where;
}
/**
* Get the orders products that have the given term
*
* @param string $term_taxonomy_id
*
* @return string
*
* @since 1.3.0
*/
private function query_order_products($term_taxonomy_id = '') {
global $wpdb;
$posts = $wpdb->posts;
$term_relationships = $wpdb->term_relationships;
$order_items = $wpdb->prefix . 'woocommerce_order_items';
$order_itemmeta = $wpdb->prefix . 'woocommerce_order_itemmeta';
$query = "
SELECT woi.order_id
FROM $order_items woi
LEFT JOIN $order_itemmeta woim
ON woi.order_item_id = woim.order_item_id
LEFT JOIN $term_relationships tr
ON tr.object_id = woim.meta_value
";
$query .= " WHERE $posts.ID = woi.order_id";
$query .= " AND woi.order_item_type = 'line_item'";
$query .= " AND woim.meta_key = '_product_id'";
if ( !empty($term_taxonomy_id) ) {
$query .= $wpdb->prepare(' AND tr.term_taxonomy_id = %d', wc_clean($term_taxonomy_id));
}
$query .= ' GROUP BY woi.order_item_id';
return $query;
}
/** Helper methods ***************************************/
/**
* Load Translations
*
* @since 1.0.0
*/
public function load_translation() {
// localization
load_plugin_textdomain( 'wc-advanced-order-filters', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/languages' );
}
/**
* Main WC_Additional_Order_Filters Instance, ensures only one instance
* is/can be loaded
*
* @return WC_Advanced_Order_Filters
*@see WC_Additional_Order_Filters()
* @since 1.1.0
*
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Cloning instances is forbidden due to singleton pattern.
*
* @since 1.1.0
*/
public function __clone() {
/* translators: Placeholders: %s - plugin name */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot clone instances of %s.', 'wc-advanced-order-filters' ), 'Additional Filters for WooCommerce Orders' ), '1.1.0' );
}
/**
* Unserializing instances is forbidden due to singleton pattern.
*
* @since 1.1.0
*/
public function __wakeup() {
/* translators: Placeholders: %s - plugin name */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot unserialize instances of %s.', 'wc-advanced-order-filters' ), 'Additional Filters for WooCommerce Orders' ), '1.1.0' );
}
/**
* Checks if current type is a shop_order.
*
* @return bool
*/
private function is_orders_page() {
global $typenow;
return 'shop_order' === $typenow;
}
}
/**
* Returns the One True Instance of WC_Additional_Order_Filters
*
* @return \WC_Advanced_Order_Filters
* @since 1.1.0
*
*/
function wc_advanced_order_filters() {
return WC_Advanced_Order_Filters::instance();
}