Woocommerce custom meta fields, sort by date


I’ve got a list of user orders, with a custom field named “Delivery Date”. This lets users pick a date when they want their order delivered. I am trying to make this custom field ‘sortable’ so the admin can sort orders by the delivery dates instead of the order date. This is the code I am using to display this custom meta field on the Orders page:

add_filter( 'manage_edit-shop_order_columns', 'bbloomer_add_new_order_admin_list_column' );
    function bbloomer_add_new_order_admin_list_column( $columns ) {
    $columns['delivery_date'] = 'Delivery Date';
    return $columns;

and this is the code I am using to make this a ‘sortable’ column:

// Sort by custom column ==> changed (working)
add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
    $custom = array(
        'delivery_date'    => 'delivery_date',
    return wp_parse_args( $custom, $columns );

Now it does make the column clickable, however it does not order the dates properly. It generates this url: edit.php?post_type=shop_order&wc-hide-notice=template_files&orderby=delivery_date&order=desc but the delivery dates still show up in the wrong order. Where am I going wrong here?

Photo for reference: https://prnt.sc/so1cgv

, CharlotteOswald 4 years 2020-05-26T07:10:20-05:00 0 Answers 100 views 0

Leave an answer