I recently upgraded a WordPress Woocommerce website to use HPOS and I found out the custom column sorting no longer works. I read that the pre_get_posts action hook no longer works with HPOS. How would I be able to get my custom delivery date and customer name columns to sort from asc to desc?
Here is some of the code currently:
// Add Custom Column Names after Order Status
add_filter( 'manage_woocommerce_page_wc-orders_columns', 'add_wc_order_list_custom_column' );
function add_wc_order_list_custom_column( $columns ) {
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key === 'order_status' ){
// Inserting after "Status" column
$reordered_columns['delivery_date'] = __( 'Delivery Date', 'my-textdomain');
$reordered_columns['customer_user'] = __( 'Customer', 'my-textdomain');
}
}
return $reordered_columns;
}
// Insert Value for Custom Columns
add_action('manage_woocommerce_page_wc-orders_custom_column', 'display_wc_order_list_custom_column_content', 10, 2);
function display_wc_order_list_custom_column_content( $column, $order ){
switch ( $column )
{
case 'delivery_date' :
// Get custom order metadata
$value = $order->get_meta('_delivery_date');
if ( ! empty($value) ) {
echo date('F j, Y', strtotime($value));
}
// For testing (to be removed) - Empty value case
else {
echo '<small>(<em>no value</em>)</small>';
}
break;
case 'customer_user' :
// Get custom order metadata
$value = $order->get_shipping_last_name();
if ( ! empty($value) ) {
echo $value;
}
// For testing (to be removed) - Empty value case
else {
echo '<small>(<em>no value</em>)</small>';
}
break;
}
}
// Make Columns Sortable
add_filter( "woocommerce_shop_order_list_table_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
$custom = array(
'delivery_date' => '_delivery_date',
'order_status' => 'order_status',
'customer_user' => 'customer_user'
);
return wp_parse_args( $custom, $columns );
}
// Sort Meta Keys and Values based on the query <---- THIS DOESN'T WORK WITH HPOS
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' );
$order_direction = $query->get( 'order' );
// Set query
if($orderby == 'ID'){
$query->set( 'meta_key', '_shipping_last_name' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', $order_direction );
}
if ( $orderby == 'customer_user' ) {
$query->set( 'meta_key', '_shipping_last_name' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', $order_direction );
}
if ( $orderby == '_delivery_date' ) {
$query->set('meta_type','DATETIME');
$query->set( 'meta_key', '_delivery_date' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', $order_direction );
}
}
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );