基于添加的自定义标题字段的WooCommerce过滤器管理员订单列表

时间:2019-04-01 08:44:47

标签: php wordpress woocommerce product hook-woocommerce

在WooCommerce中,我在管理订单列表中添加了一个新的自定义列“ Language”,其自定义值为“ Japanese”或“ Non Japanese”。

如何为日语和非日语添加过滤器选项。

enter image description here

我使用以下代码添加了一个过滤器下拉列表:

add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
function wpse45436_admin_posts_filter_restrict_manage_posts(){
    global $post_type;
    if( $post_type == 'shop_order' ) {
        //change this to the list of values you want to show
        //in 'label' => 'value' format
        $values = array(
            'Japanese' => 'Japanese', 
            'Non Japanese' => 'Non Japanese',
        );
        ?>
        <select name="ADMIN_FILTER_FIELD_VALUE">
        <option value=""><?php _e('Filter By ', 'wose45436'); ?></option>
        <?php
            $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
            foreach ($values as $label => $value) {
                printf (
                    '<option value="%s"%s>%s</option>',
                    $value,
                    $value == $current_v? ' selected="selected"':'',
                    $label
                );
            }
        ?>
        </select>
        <?php
    }
}

我还添加了一些用于过滤订单列表的代码,但是它不起作用,这是我的代码:

add_action( 'pre_get_posts', 'apply_my_custom_product_filters' );
function apply_my_custom_product_filters( $query ) {

    global $pagenow;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['ADMIN_FILTER_FIELD_VALUE'] ) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '' && $_GET['post_type'] == 'shop_order' ) {

       $meta_key_query = array(
          array(
              'meta_key'     => 'order_add_language',
              'value'   => esc_attr( $_GET['ADMIN_FILTER_FIELD_VALUE'] ),
              'compare' => '=',
          ),
          'posts_per_page' => 10,
          'paged' => $paged,
      );

      $query->set( 'meta_query', $meta_key_query );
    }
}

2 个答案:

答案 0 :(得分:0)

这在您的meta_key_query中是错误的。您可以尝试以下代码。

add_action( 'pre_get_posts', 'apply_my_custom_product_filters' );
function apply_my_custom_product_filters( $query ) {

    global $pagenow;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
    if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['ADMIN_FILTER_FIELD_VALUE'] ) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '' && $_GET['post_type'] == 'shop_order' ) {

      $meta_key_query = array(
        array(
          'meta_key'     => 'order_add_language',
          'value'   => esc_attr( $_GET['ADMIN_FILTER_FIELD_VALUE'] ),
        )
      );
      $query->set( 'meta_query', $meta_key_query );

    }

}

答案 1 :(得分:0)

您的代码中有一些错误...请尝试以下重新访问的代码:

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

    if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {
        $domain    = 'woocommerce';
        $languages = array( __('Japanese', $domain) => __('Non Japanese', $domain) );
        $current   = isset($_GET['filter_shop_order_language'])? $_GET['filter_shop_order_language'] : ''

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

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

add_action( 'pre_get_posts', 'process_admin_shop_order_language_filter' );
function process_admin_shop_order_language_filter( $query ) {
    global $pagenow;

    if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['filter_shop_order_language'] ) 
        && $_GET['filter_shop_order_language'] != '' && $_GET['post_type'] == 'shop_order' ) {

        $meta_query = $query->get( 'meta_query' ); // Get the current "meta query"

        $meta_query[] = array( // Add to "meta query"
            'meta_key' => 'order_add_language',
            'value'    => esc_attr( $_GET['filter_shop_order_language'] ),
        );
        $query->set( 'meta_query', $meta_query ); // Set the new "meta query"

        $query->set( 'posts_per_page', 10 ); // Set "posts per page"

        $query->set( 'paged', ( get_query_var('paged') ? get_query_var('paged') : 1 ) ); // Set "paged"
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。