• I’m trying to sort my Woocommerce orders by _payment_method on the ‘Orders’ admin screen (edit.php?post_type=shop_order).

    So far I am able to sort by ‘Pay_by_phone’, but it sorts everytime the page loads, which of course is not ideal.

    function filter_orders_pay_by_phone($query) {
        global $pagenow;
        $qv = &$query->query_vars;
    
        if ( $pagenow == 'edit.php' &&
            isset($qv['post_type']) && $qv['post_type'] == 'shop_order' ) {
            $query->set('meta_key', '_payment_method');
            $query->set('meta_value', 'Pay by phone');
        }
    
        return $query;
    }
    add_filter('pre_get_posts', 'filter_orders_pay_by_phone');

    I need to insert a button, or another sort /filter option to this admin page called “Phone Orders” (the current sort options are All, Trash, Processing etc…) that upon click causes a page refresh and adds this filter so only those orders are displayed.

    I am unaware of any hooks that I can use to generate this functionality. Any guidance is appreciated. Thanks

    https://wordpress.org/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey there! I am also having problems but of a much simpler kind… any chance you could help me with my problem? I have a large section of space between my header and my shop that I can’t seem to get rid of!

    @steveyp
    As per forum guidelines, please do not hijack someone else’s thread.

    Here I created a function to get all possible meta_values of a certain meta_key. Then I hooked restrict_manage_posts to add the new select field of meta_values to the filter form. Finally I hooked parse_query to add the new filter to the post query.

    // function to grab all possible meta values of the chosen meta key in thi scase '_payment_method'
        function get_meta_values( $meta_key,  $post_type = 'post' ) {
    
            $posts = get_posts(
                array(
                    'post_type' => $post_type,
                    'meta_key' => $meta_key,
                )
            );
    
            $meta_values = array();
            foreach( $posts as $post ) {
                $meta_values[] = get_post_meta( $post->ID, $meta_key, true );
            }
    
            return $meta_values;
    
        }
    
        //Hook the filter options form
        add_action('restrict_manage_posts','add_meta_value_to_posts');
    
        function add_meta_value_to_posts(){
    
            // only add filter to shop_order
            global $post_type;
            if( $post_type == 'shop_order' ) {
    
                // function to grab all possible meta values of the chosen meta key in this case '_payment_method'
                $meta_values = get_meta_values('_payment_method', 'shop_order');
    
                // Generate select field from meta values
                echo '<select name="_payment_method" id="_payment_method">';
    
                    $all_selected = sanitize_text_field($_GET['_payment_method']) == 'all' ? ' selected' : '';
                    echo '<option value="all"'.$all_selected.'>All</option>';
    
                    foreach ( $meta_values as $meta_value ) {
                        $selected = sanitize_text_field($_GET['_payment_method']) == $meta_value ? ' selected' : '';
                        echo '<option value="'.$meta_value.'"'.$selected.'>'.$meta_value.'</option>';
                    }
    
                echo '</select>';
    
            }
    
        }
    
        // Hook parse_query to add new filter parameters
        add_action('parse_query','filter_posts_per_meta_value');
    
        function filter_posts_per_meta_value( $query ) {
    
            global $pagenow, $post_type;
            // Only add parmeeters if on shop_order and if all is not selected
            if( $pagenow == 'edit.php' && $post_type == 'shop_order' && !empty($_GET['_payment_method']) && $_GET['_payment_method'] != 'all' ) {
    
                $query->query_vars['meta_query'][] = array(
                    'key' => '_payment_method',
                    'value' => $_GET['_payment_method'],
                    'compare' => '=',
                );
    
            }
    
        }

    Sorry posts_per_page should be added to the get_posts args, most likely you want to query all like this:

    $posts = get_posts(
                array(
                    'post_type' => $post_type,
                    'meta_key' => $meta_key,
                    'posts_per_page' => -1,
                )
            );
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Filter / sort orders by Post Meta on order admin page’ is closed to new replies.