• Resolved petermorlion

    (@petermorlion)


    Is there a way to search all my orders by order total (the _order_total meta field)? Either built-in, by plugin or by code.

    I’ve written some code myself:

    add_action('pre_get_posts', 'search_by_amount', 100, 1);
    
    function search_by_amount($query)
    {
        global $pagenow, $post_type;
    
        if (!is_admin()) {
            return;
        }
    
        if ($pagenow !== 'edit.php') {
            return;
        }
    
        if (
            $post_type == 'shop_order'
            || (is_array($query->query_vars['post_type']) && in_array('shop_order', $query->query_vars['post_type']))
            || $query->query_vars['post_type'] == 'shop_order'
        ) {
    
            $s = $_GET['s'];
    
            // Check if search term can be a float
            if ($s == (string)(float)$s) {
                $query->query_vars['meta_query'][] = array(
                    'key'     => '_order_total',
                    'value'   => floatval($s),
                    'compare' => '=',
                    'type'    => 'NUMERIC'
                );
            }
        }
    }

    Unfortunately, this doesn’t seem to work. I’ve tried different meta queries, but whenever I search for a numeric value, I don’t get a single result.

    Does anyone know if/how this can be achieved?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Search by order total’ is closed to new replies.