Woocommerce Get Orders By Meta Value

Question

I have orders, some of them has meta value is set 0. Please see my attachment

To filter out orders based on meta value, i am using below code…

add_action( 'pre_get_posts', function( $query ) {

global $pagenow;

$meta_query = $query->get('meta_query') ? $query->get('meta_query') : array();

if ( $query->is_admin && $pagenow == 'edit.php' && $_GET['post_type'] == 'shop_order' ) {

    if ( isset( $_GET['outlet_name'] ) && ! empty( $_GET['outlet_name'] ) ) {

        if ( esc_attr( $_GET['outlet_name'] ) == 'default' ) {  

            $meta_query[] = array( // Add to "meta query"
                'meta_key' => '_pos_order_warehouse',
                'value'    => 1,
                'compare'  => '<',
            );

        }else{                

            $meta_query[] = array( // Add to "meta query"
                'meta_key' => '_pos_order_warehouse',
                'value'    => esc_attr( $_GET['outlet_name'] ),
            );
        }
    }

    if ( ! empty( $meta_query ) ) {

        $query->set( 'meta_query', $meta_query ); // Set the new "meta query"

        $query->set( 'posts_per_page', 10 ); // Set "posts per page"

        $query->set( 'paged', ( get_query_var('paged') ? get_query_var('paged') : 1 ) ); // Set "paged"
    }
}
});

But above query is returning all order when i send $_GET['outlet_name'] as ‘default’

Why isn’t the query working?

0
Sajjad Hossain Sagor 8 months 2020-11-29T16:10:19-05:00 0 Answers 9 views 0

Leave an answer

Browse
Browse