I’ve been using the following code to display custom columns in the Woocommerce admin orders page:
// Add Order notes Column to Admin Orders Page
add_filter( 'manage_woocommerce_page_wc-orders_columns', 'woocommerce_add_order_notes_column' );
function woocommerce_add_order_notes_column( $columns ) {
return array_slice( $columns, 0, 8, true )
+ array( 'order_notes' => __( 'Order Notes', 'woocommerce' ) )
+ array_slice( $columns, 8, NULL, true );
return $ordered_columns;
}
// Get Order notes
add_action( 'manage_woocommerce_page_wc-orders_custom_column' , 'woocommerce_show_order_notes_column', 10, 2 );
function woocommerce_show_order_notes_column( $column_name, $order_id ) {
switch ( $column_name ) {
case 'order_notes':
$order = wc_get_order( $order_id );
$note = $order->get_customer_note();
if ( !empty($note) ) {
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
} else {
echo '<span class="na">–</span>';
}
break;
}
}
// Add 'Pre-order Products' Column to Admin Orders Page
add_filter( 'manage_woocommerce_page_wc-orders_columns', 'woocommerce_add_preorder_products_tick_column' );
function woocommerce_add_preorder_products_tick_column( $columns ) {
$ordered_columns = array();
foreach( $columns as $key => $column ){
$ordered_columns[$key] = $column;
if( 'wc_actions' == $key ){
$ordered_columns['preorder_products_tick'] = __( 'Pre-orders', 'woocommerce');
}
}
return $ordered_columns;
}
// Get Pre-order Products
add_action( 'manage_woocommerce_page_wc-orders_custom_column' , 'add_items_to_preorder_products_tick_column' );
function add_items_to_preorder_products_tick_column( $colname ) {
global $the_order;
if( $colname == 'preorder_products_tick' ) {
// get items from the order global object
$order_items = $the_order->get_items();
if ( !is_wp_error( $order_items ) ) {
foreach( $order_items as $order_item ) {
$product_id = $order_item->get_product_id();
if ( has_term( array( 'comic-book-pre-orders', 'dc-comics-pre-orders', 'image-comics-pre-orders', 'marvel-comics-pre-orders', 'other-publisher-pre-orders' ), 'product_cat', $product_id ) ) {
echo "✔";
}
}
}
}
}
// Add 'Total Sales' column to the products list
// add column
add_filter( 'manage_edit-product_columns', 'column_total_sales_1', 20 );
// populate column
add_action( 'manage_posts_custom_column', 'column_total_sales_2' );
// make column sortable
add_filter('manage_edit-product_sortable_columns', 'column_total_sales_3');
// how to sort column
add_action( 'pre_get_posts', 'column_total_sales_4' );
function column_total_sales_1( $columns_array ) {
return array_slice( $columns_array, 0, 8, true )
+ array( 'total_sales' => 'Total Sales' )
+ array_slice( $columns_array, 8, NULL, true );
}
function column_total_sales_2( $column_id ) {
if( $column_id == 'total_sales' )
echo get_post_meta( get_the_ID(), 'total_sales', true );
}
function column_total_sales_3( $a ){
return wp_parse_args( array( 'total_sales' => 'by_total_sales' ), $a );
}
function column_total_sales_4( $query ) {
if( !is_admin() || empty( $_GET['orderby']) || empty( $_GET['order'] ) )
return;
if( $_GET['orderby'] == 'by_total_sales' ) {
$query->set('meta_key', 'total_sales' );
$query->set('orderby', 'meta_value_num');
$query->set('order', $_GET['order'] );
}
return $query;
}
// Make stock column sortable
add_filter( 'manage_edit-product_sortable_columns', 'wcss_make_stock_sortable' );
function wcss_make_stock_sortable( $sortable_columns ) {
$sortable_columns[ 'is_in_stock' ] = '_stock';
return $sortable_columns;
}
I’m now in the process of updating to High-Performance Order Storage (HPOS) and this code no longer works.
Can anyone advise me how I can update the code to work with HPOS please, as I’m completely lost!
Any advise would be very much appreciated, thank you!