Filter orders by specific meta fields in WooCommerce admin orders list

牧云@^-^@ 提交于 2020-06-22 22:57:06

问题


Can anyone let me know, how can i add / set filter by company name in woo-commerce order page.

and please share functions or show my error so i can solved it.

i tried it but not working. you help is much appreciated.

add_action( 'restrict_manage_posts', 'admin_shop_order_by_product_type_filter' );
function admin_shop_order_by_product_type_filter(){
    global $pagenow, $post_type;

    if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {
        $domain     = 'woocommerce';
        $filter_id  = 'filter_billing_company';
        $current    = isset($_GET[$filter_id])? $_GET[$filter_id] : '';
        $query_args = ['fields' => '_billing_company', 'orderby' => 'order'];

        echo "<pre>";print_r(get_terms($query_args)); echo "</pre>";

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

        foreach ( get_terms($query_args) as $term_name ) {
            printf( '<option value="%s"%s>%s</option>', $term_name,
                $term_name === $current ? '" selected="selected"' : '', ucfirst($term_name) );
        }
        echo '</select>';
    }
}


Thanks


回答1:


To filter orders by meta fields on admin orders list, you will use the following (where you will define in the 1st function below the metakey / label pairs that will filter orders):

// Custom function where metakeys / labels pairs are defined
function get_filter_shop_order_meta( $domain = 'woocommerce' ){
    // Add bellow the metakey / label pairs to filter orders
    return [
        '_billing_company' => __('Billing company', $domain),
        '_order_total'     => __('Gran total', $domain), 
    ];
}

// Add a dropdown to filter orders by meta
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="">' . __('Filter by meta…', $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>';
    }
}

// Process the filter dropdown for orders by Marketing optin
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']    = 'meta_value';
    }
    return $vars;
}

// (Optional) Make a custom meta field searchable from the admin order list search field
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;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.



来源:https://stackoverflow.com/questions/62209407/filter-orders-by-specific-meta-fields-in-woocommerce-admin-orders-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!