0
%
1
0
0
Back To Top

How to add custom code to filter orders in Woocommerce dashboard

Add this code in the functions.php of your theme or child-theme file. This code generates at first a dropdown menu and populates it with all shipping methods you have on your store. Then I queries the DB to get the order IDs with selected shipping method in the dropdown (tested and works!).

// Filter by shipping methods woo dashboard

add_action( ‘restrict_manage_posts’, function () {
if ( is_admin() && ! empty( $_GET[‘post_type’] ) && $_GET[‘post_type’] == ‘shop_order’ ) {
$exp_types = array();
$zones = WC_Shipping_Zones::get_zones();

foreach ( $zones as $z ) {
foreach ( $z[‘shipping_methods’] as $method ) {
$exp_types[ $method->instance_id ] = $method->title;
}
}
?>
<select name=”shipping_method”>
<option value=””><?php _e( ‘Filtrer par Pickup/Livaison’, ‘woocommerce’ ); ?></option>
<?php
$current_v = isset( $_GET[‘shipping_method’] ) ? $_GET[‘shipping_method’] : ”;
foreach ( $exp_types as $key => $label ) {
printf( ‘<option value=”%s”%s>%s</option>’, $key, $key == $current_v ? ‘ selected=”selected”‘ : ”, $label );
}
?>
</select>
<?php
}
} );

add_filter( ‘posts_where’, function ( $where, $wp_query ) {
global $pagenow, $wpdb;
$method = $_GET[‘shipping_method’];

if ( is_admin() && $pagenow == ‘edit.php’ && $wp_query->query_vars[‘post_type’] == ‘shop_order’ && ! empty( $method ) ) {
$where .= $wpdb->prepare( “AND ID
IN (
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_itemmeta m
LEFT JOIN {$wpdb->prefix}woocommerce_order_items i
ON i.order_item_id = m.order_item_id
WHERE meta_key = ‘instance_id’ and meta_value = ‘{$method}’ )” );
}
return $where;
}, 10, 2 );