Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

php - Add a sortable custom column in Woocommerce Admin Orders list

I've added a custom column to the "Orders" section of WooCommerce for the shipping zip code. The column and its values appear correctly.

What I cannot figure out is how to make the sorting of this field work (clicking on the column header). Other code examples I could find mention using the hook "manage_edit-shop_order_sortable_columns", but this doesn't seem to be working for this field.

Note: I've seen the other StackOverflow issues about this, but none seem to have sorting working.

/**
 * ADD ZIP CODE TO WOOCOMMERCE ORDERS LIST
 */

// Add column (working)
add_filter( 'manage_edit-shop_order_columns', 'custom_woo_columns_function' );
function custom_woo_columns_function( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    unset( $new_columns[ 'order_actions' ] );

    // all of your columns will be added before the actions column
    $new_columns['zipcode'] = 'Zip Code';

    //stop editing
    $new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
    return $new_columns;
}

// Change order of columns (working)
add_action( 'manage_shop_order_posts_custom_column', 'custom_woo_admin_value', 2 );
function custom_woo_admin_value( $column ) {
    global $post;
    $zip_value = get_post_meta($post->ID, '_shipping_postcode', true);
    if ( $column == 'zipcode' ) {
        echo ( isset( $zip_value ) ? $zip_value : '' );
    }
}

// Sort by custom column (NOT WORKING)
add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
    $custom = array(
        'zipcode'    => '_shipping_postcode',
    );
    return wp_parse_args( $custom, $columns );
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update 2020:

You can try this too which make it sortable and searchable:

// Add a custom column
add_filter( 'manage_edit-shop_order_columns', 'add_custom_shop_order_column' );
function add_custom_shop_order_column( $columns ) {
    $order_actions = $columns['order_actions'];
    unset($columns[ 'order_actions' ]);

    // add custom column
    $columns['postcode'] = __('Zip Code', 'woocommerce');

    // Insert back 'order_actions' column
    $columns['order_actions'] = $order_actions;

    return $columns;
}

// Custom column content
add_action( 'manage_shop_order_posts_custom_column', 'shop_order_column_meta_field_value' );
function shop_order_column_meta_field_value( $column ) {
    global $post, $the_order;

    if ( ! is_a( $the_order, 'WC_Order' ) ) {
        $the_order = wc_get_order( $post->ID );
    }

    if ( $column == 'postcode' ) {
        echo $the_order->get_shipping_postcode();
    }
}

// Make custom column sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'shop_order_column_meta_field_sortable' );
function shop_order_column_meta_field_sortable( $columns )
{
    $meta_key = '_shipping_postcode';
    return wp_parse_args( array('postcode' => $meta_key), $columns );
}

// Make sorting work properly (by numerical values)
add_action('pre_get_posts', 'shop_order_column_meta_field_sortable_orderby' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
    global $pagenow;

    if ( 'edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] ){

        $orderby  = $query->get( 'orderby');
        $meta_key = '_shipping_postcode';

        if ('_shipping_postcode' === $orderby){
          $query->set('meta_key', $meta_key);
          $query->set('orderby', 'meta_value_num');
        }
    }
}

// Make metakey searchable in the shop orders list
add_filter( 'woocommerce_shop_order_search_fields', 'shipping_postcode_searchable_field' );
function shipping_postcode_searchable_field( $meta_keys ){
    $meta_keys[] = '_shipping_postcode';
    return $meta_keys;
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...