Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - Make custom column with billing last name sortable in WooCommerce order admin list

I am trying to sort all my orders in alphabetical order in a custom column that I have built. Right now it can only be sorted asc and desc by the time when order was created.

I have built a custom column for user last name, I have passed billing last name inside that column.

Right now I am tryin to make it sort in alphabetical order. I am using the following:

function add_order_new_column_header( $columns ) {

  $new_columns = array();

    foreach ( $columns as $column_name => $column_info ) {

        $new_columns[ $column_name ] = $column_info;

        if ( 'order_total' === $column_name ) {
            $new_columns['billing_info'] = __( 'Billing Info', 'my-textdomain' );
        }
    }

   return $new_columns;

 }
 add_filter( 'manage_edit-shop_order_columns', 'add_order_new_column_header', 20);

 add_action( 'manage_shop_order_posts_custom_column', 'add_wc_order_admin_list_column_content' );

 function add_wc_order_admin_list_column_content( $column ) {

    global $post;

    if ( 'billing_info' === $column ) {

       $order = wc_get_order( $post->ID );
       echo '<p>' . $order->get_billing_last_name() . '</p>';
  
    }
 }

add_filter( 'manage_edit-shop_order_sortable_columns', 'my_sortable_name_column' );
function my_sortable_name_column( $columns ) {
    $columns['billing_info'] = 'billing_last_name';
    return $columns;
}
add_action( 'pre_get_posts', 'name_filter' );

function name_filter( $query ) {
// if it is not admin area, exit the filter immediately
if ( ! is_admin() ) return;

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

if( $orderby == 'billing_info' ) {
    $query->set('meta_key', 'billing_last_name' );
    $query->set('orderby', 'meta_value');// or meta_value_num
    $query->set('order', 'ASC');
}

return $query;
}

The custom column is sortable, so that works but the sort order I want is not applied in a correct way. Someone who can tell me why?


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

1 Answer

0 votes
by (71.8m points)

You have some minor bugs in your pre_get_posts function, also the correct meta key is _billing_last_name, not billing_last_name.

So use the following instead:

  • Explanation via comment tags added in the code
// Add a header
function filter_manage_edit_shop_order_columns( $columns ) {
    $new_columns = array();

    // Loop trough columns
    foreach ( $columns as $column_name => $column_info ) {

        $new_columns[ $column_name ] = $column_info;

        // Compare, add after
        if ( $column_name === 'order_total' ) {
            $new_columns['billing_info'] = __( 'Billing info', 'woocommerce' );
        }
    }

   return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Populate the custom column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
    // Compare
    if ( $column == 'billing_info' ) {      
        // Get an instance of the WC_Order object from an Order ID
        $order = wc_get_order( $post_id );
        
        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {         
            // Get billing last name
            $billing_last_name = $order->get_billing_last_name();
                
            // NOT empty
            if ( ! empty ( $billing_last_name ) ) {
                echo '<p>' . $billing_last_name . '</p>';
            }
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

// Make custom column sortable
function filter_manage_edit_shop_order_sortable_columns( $sortable_columns ) {  
    return wp_parse_args( array( 'billing_info' => '_billing_last_name' ), $sortable_columns );
}
add_filter( 'manage_edit-shop_order_sortable_columns', 'filter_manage_edit_shop_order_sortable_columns', 10, 1 );

// Orderby for custom column
function action_pre_get_posts( $query ) {   
    // If it is not admin area, exit
    if ( ! is_admin() ) return;
    
    global $pagenow;

    // Compare
    if ( $pagenow === 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] === 'shop_order' ) {      
        // Get orderby
        $orderby = $query->get( 'orderby' );

        // Set query
        if ( $orderby == '_billing_last_name' ) {           
            $query->set( 'meta_key', '_billing_last_name' );
            $query->set( 'orderby', 'meta_value' );
        }
    }
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...