query – Filter orders by modify date

Question

I’m trying to add a new filter that will sort orders by modification date.
Below is the code I found and modified. Unfortunately, filtering doesn’t work, returns zero results.I think the problem is with $filter_id

function get_filter_shop_order_meta( $domain = 'woocommerce' ){
    // Add below the metakey / label pairs to filter orders
    return [
        'last_modified' => __('Recently updated', $domain)
    ];
}


add_action( 'restrict_manage_posts', 'display_admin_shop_order_by_meta_filter' );
function display_admin_shop_order_by_meta_filter(){
    global $pagenow, $typenow;

    if( 'shop_order' === $typenow && 'edit.php' === $pagenow ) {
        $domain    = 'woocommerce';
        $filter_id = 'filter_shop_order_by_meta';
        $current   = isset($_GET[$filter_id])? $_GET[$filter_id] : '';

        echo '<select name="'.$filter_id.'">
        <option value="">' . __('Sort by…', $domain) . '</option>';

        $options = get_filter_shop_order_meta( $domain );

        foreach ( $options as $key => $label ) {
            printf( '<option value="%s"%s>%s</option>', $key, 
                $key === $current ? '" selected="selected"' : '', $label );
        }
        echo '</select>';
    }
}


add_filter( 'request', 'process_admin_shop_order_marketing_by_meta', 99 );
function process_admin_shop_order_marketing_by_meta( $vars ) {
    global $pagenow, $typenow;
    
    $filter_id = 'filter_shop_order_by_meta';

    if ( $pagenow == 'edit.php' && 'shop_order' === $typenow 
    && isset( $_GET[$filter_id] ) && ! empty($_GET[$filter_id]) ) {
        $vars['meta_key']   = $_GET[$filter_id];
        $vars['orderby']    = 'ASC';
    }
    return $vars;
}


add_filter( 'woocommerce_shop_order_search_fields', 'shop_order_meta_search_fields', 10, 1 );
function shop_order_meta_search_fields( $meta_keys ){
    foreach ( get_filter_shop_order_meta() as $meta_key => $label ) {
        $meta_keys[] = $meta_key;
    }
    return $meta_keys;
}

I will be grateful for your help.

0
M5_Crate 2 months 2021-09-23T16:48:53-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse