对于路过的人来说,把所有这些代码放在一起并不是一件小事。问题实际上是缺乏数据。我解决了将数据添加为元键(在我的情况下,当订单状态改变时添加)并将其作为列值进行响应的问题。因此,完整代码是:
// Adding a new column to ADMIN order list
add_filter( \'manage_edit-shop_order_columns\', \'custom_shop_order_column\', 20 );
function custom_shop_order_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[\'closing_date_column\'] = __( \'Validación\',\'theme_domain\');
}
}
return $reordered_columns;
}
// Echoing custom fields meta data for new column in ADMIN order list
add_action( \'manage_shop_order_posts_custom_column\' , \'custom_orders_list_column_content\', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
switch ( $column )
{
case \'closing_date_column\' :
echo get_post_meta( $post_id, \'closing_date_column\', true );
break;
}
}
// 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 = \'closing_date_column\';
return wp_parse_args( array(\'closing_date_column\' => $meta_key), $columns );
}
// Make sorting work properly for date field
add_action(\'pre_get_posts\', \'shop_order_column_meta_field_sortable_orderby\' );
function shop_order_column_meta_field_sortable_orderby( $query ) {
global $pagenow;
$orderby = $query->get( \'orderby\');
if (\'closing_date_column\' === $orderby){
$query->set(\'meta_key\',\'closing_date_column\');
$query->set(\'orderby\', \'meta_value_date\');
}
}
// add closing date to order meta when it changes status
// The format is "Y-m-d" for sorting doesn\'t work with any date format
add_action( \'woocommerce_order_status_changed\', \'action_woocommerce_order_status_changed\', 10, 4 );
function action_woocommerce_order_status_changed( $this_get_id, $this_status_transition_from, $this_status_transition_to, $instance ) {
update_post_meta( $this_get_id, \'closing_date_column\', date("Y-m-d") );
};