php - Making custom column sortable in WooCommerce admin order list
Get the solution ↓↓↓I have my custom column showing data, but I just can't seem to get it to sort. At the moment, if I press the column title for sorting, it only sorts by the date of the orders, rather than the data from this new column. I am especially stuck on what the "value" should be in the array for "meta_query" – everything I have tried hasn't worked. Any help would be much appreciated!
// ----- add column to orders that shows the booking date -----
function ec_order_items_column2($columns) {
$new_columns = array();
foreach($columns as $key=>$column){
$new_columns[$key] = $columns[$key];
if($key === 'ordered_products') {
$new_columns['start_date'] = __('Booking Time','woo-custom-ec2');
}
}
return $new_columns;
}
add_filter('manage_edit-shop_order_columns', 'ec_order_items_column2', 99 );
function ec_order_items_column_cnt2($column) {
global $post, $the_order;
$order_id = $the_order->get_id();
// Replace 'my_custom_column_key' with your desired column key
if ( 'start_date' === $column ) {
$display = array();
// Loop through order items
foreach ( $the_order->get_items() as $item_id => $item ) {
// $item_id is the current Item ID
// Get the WC_Product Object
$product = $item->get_product();
// Get custom order item meta data
$meta_value = $item->get_meta('phive_display_time_from');
$display[$item_id] = $meta_value;
}
// Testing output
echo implode(' | ', $meta_value);
}
}
add_action('manage_shop_order_posts_custom_column', 'ec_order_items_column_cnt2', 99);
// ---- make sortable column ----
add_filter( 'manage_edit-shop_order_sortable_columns', 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
$custom = array(
'start_date' => '_phive_display_time_from',
);
return wp_parse_args( $custom, $columns );
}
add_action('pre_get_posts', 'custom_booking_orderby');
function custom_booking_orderby( $query ) {
if ( !is_admin() ){ return; }
$orderby = $query->get( 'orderby');
if ('phive_display_time_from' == $orderby){
$query->set('meta_key','phive_display_time_from');
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_query', array(
array(
'key' => 'phive_display_time_from',
'value' => 'meta_value',
)
));
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: npm err! code 1
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.