Я использовал следующий код для отображения пользовательских столбцов на странице заказов администратора Woocommerce:
// 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;
}
Сейчас я обновляюсь до высокопроизводительного хранилища заказов (HPOS), и этот код больше не работает.
Может ли кто-нибудь посоветовать мне, как обновить код для работы с HPOS?
Любой совет будет очень признателен, спасибо!


Хорошо, мне удалось заставить столбцы «Предварительные заказы» и «Примечания к заказу» работать с этим кодом:
// Legacy – for CPT-based orders
add_filter( 'manage_edit-shop_order_columns', 'woocommerce_add_order_notes_column' );
// HPOS - based orders
add_filter( 'manage_woocommerce_page_wc-orders_columns', 'woocommerce_add_order_notes_column' );
function woocommerce_add_order_notes_column( $columns ) {
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if ( $key === 'order_total' ){
// Inserting after "Status" column
$reordered_columns['preorder_products_tick'] = __( 'Pre-orders','theme_domain');
$reordered_columns['order_notes'] = __( 'Order Notes','theme_domain');
}
}
return $reordered_columns;
}
// Get Order notes
// Legacy – for CPT-based orders
add_action( 'manage_shop_order_posts_custom_column' , 'woocommerce_show_order_notes_column', 10, 2 );
// HPOS - based orders
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_or_order_id ) {
// legacy CPT-based order compatibility
$order = $order_or_order_id instanceof WC_Order ? $order_or_order_id : wc_get_order( $order_or_order_id );
switch ( $column_name ) {
case 'order_notes' :
// Get custom order metadata
$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;
case 'preorder_products_tick' :
$order_items = $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 "✔";
}
}
}
break;
}
}
И я нашел ответ на столбец общего объема продаж здесь [Столбцы в WooCommerce][1], который я немного изменил:
// Add 'Total Sales' column to the products list
add_filter( 'manage_edit-product_columns', 'add_custom_total_sales_column' );
function add_custom_total_sales_column( $column_name ) {
return wp_parse_args(
array( 'total_sales' => 'Total Sales' ),
$column_name
);
}
// Populate 'Total Sales' column
add_action( 'manage_posts_custom_column', 'populate_custom_total_sales_column' );
function populate_custom_total_sales_column( $column_id ) {
if ( $column_id == 'total_sales' )
echo get_post_meta( get_the_ID(), 'total_sales', true );
}
// Make 'Total Sales' column Sortable
add_filter('manage_edit-product_sortable_columns', 'sortable_custom_total_sales_column');
function sortable_custom_total_sales_column( $a ){
return wp_parse_args( array( 'total_sales' => 'by_total_sales' ), $a );
}
// Sort 'Total Sales' column
add_action( 'pre_get_posts', 'sort_custom_total_sales_column' );
function sort_custom_total_sales_column( $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;
}
Надеюсь, это поможет любому, кто столкнулся с той же проблемой, что и я :) [1]: https://rudrastyh.com/woocommerce/columns.html