Woocommerce Filter orders by shipping method

Question

My current code shows the proper options in the drop down but for some reason the filter is still not working correct. It still shows all the orders when using this filter. Running the query against the database directly looks to be giving me the correct results. Our shipping options do have spaces in their name like "Flat Rate" if this matters. Maybe I am missing something?

function admin_shipping_filter( $where, &$wp_query )
{
    global $pagenow;
    $method = $_GET['shipping_filter'];

    if ( is_admin() && $pagenow=='edit.php' && $wp_query->query_vars['post_type'] == 'shop_order' && !empty($method) ) {
        $where .= $GLOBALS['wpdb']->prepare( 'AND ID
                            IN (
                                SELECT order_id
                                FROM wp_lt_woocommerce_order_items
                                WHERE order_item_type = "shipping"
                                AND order_item_name = "' . $method . '"
                            )' );
    }

    return $where;
}

function display_shipping_dropdown(){

    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->title;
            }
        }

        ?>
        <select name="shipping_method">
            <option value=""><?php _e('All Shipping  Methods', 'woocommerce'); ?></option>
            <?php
            $current_v = isset($_GET['shipping_method']) ? $_GET['shipping_method'] : '';
            foreach ($exp_types as $label) {
                printf
                (
                    '<option value="%s"%s>%s</option>',
                    $label,
                    $label == $current_v? ' selected="selected"':'',
                    $label
                );
            }
            ?>
        </select>
        <?php
    }
}
add_action( 'restrict_manage_posts', 'display_shipping_dropdown' );```
0
SSSlippy 2 months 0 Answers 8 views 0

Leave an answer