TransWikia.com

Woocommerce Filter orders by shipping method

WordPress Development Asked by SSSlippy on February 16, 2021

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' );```

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP