通过WooCommerce管理订单列表中的特定元字段过滤订单

问题描述 投票:3回答:1

谁能告诉我,如何在woo-commerce订单页面中按公司名称添加/设置过滤器。

并且请共享功能或显示我的错误,以便我可以解决它。

我尝试过,但是没有用。非常感谢您的帮助。

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>';
    }
}


谢谢

php wordpress woocommerce backend orders
1个回答
1
投票

要通过管理订单列表上的元字段过滤订单,您将使用以下(您将在第一个函数中定义要过滤订单的metakey /标签对下面的位置

// 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;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.